2015-07-27 14 views
14

Ecco il testo:parte in grassetto del stringa nel UITextView rapida

@IBOutlet weak var legalText: UITextView! 
let textToAppend = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." 
legalText.text = legalText.text.stringByAppendingString(textToAppend) 

voglio bold "Termini di servizio" e "NOTA: I Higo Termini di servizio come indicato qui di seguito è valido a partire dalla ' Data ultimo aggiornamento 'precedente per qualsiasi utente che sta navigando nel sito Web HIGO o per qualsiasi utente che crea un account HIGO in data successiva o successiva. "

provo con l'utilizzo di codice in UILabel UITextView, ma non ha funzionato:

var termsofservice : UILabel = UILabel() 
termsofservice.numberOfLines = 0 
termsofservice.text = "TERMS OF SERVICE" 
termsofservice.font = UIFont.boldSystemFontOfSize(20) 
termsofservice.textAlignment = NSTextAlignment.Left; 

var pleasenote : UILabel = UILabel() 
pleasenote.numberOfLines = 0 
pleasenote.text = "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." 
pleasenote.font = UIFont.boldSystemFontOfSize(20) 
pleasenote.textAlignment = NSTextAlignment.Left; 

let textToAppend = "\(termsofservice)\nLast Updated: May 7, 2015\n\n\(pleasenote)" 

anche provare con questi, ma non ha funzionato, ma solo mostrare "Termini di servizio" e "ultimo aggiornamento ..", ha fatto non mostrare "PER FAVORE NOTA ..."

var termsofservice = "TERMS OF SERVICE" 
var normalText = "\n\nLast Updated: May 7, 2015" 
var pleasenote = "\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." 

var attributedString = NSMutableAttributedString(string:normalText) 

var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)] 
var boldString = NSMutableAttributedString(string:pleasenote, attributes:attrs) 
var boldString0 = NSMutableAttributedString(string:termsofservice, attributes:attrs) 

boldString0.appendAttributedString(attributedString) 
attributedString.appendAttributedString(boldString) 
legalText.attributedText = boldString0 

Come evidenziare quella parte di stringa?

Nota: il testo è ancora lungo e contiene più parti di stringa in grassetto.

+1

si dovrebbe usare attribuito stringa – Miknash

+3

possibile duplicato [Rendere il testo in grassetto usando la stringa attribuita in swift] (http://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string-in-swift) – Larme

+0

come? Per favore mi mostri. – Sarimin

risposta

30

aggiornato per Swift 3

func attributedText() -> NSAttributedString { 

    let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString 

    let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)]) 

    let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)] 

    // Part of string to be bold 
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE")) 
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:")) 

    // 4 
    return attributedString 
} 

e SET attributext testo per la visualizzazione del testo come:

legalText.attributedText = attributedText() 
+0

Questo è quasi corretto, ma 'rangeOfString' restituisce' Range', e 'addAttributes' ha bisogno di un' NSRange', quindi è necessario giocarci un po. Ad ogni modo, +1 –

+2

Se userai la stringa come NSString 'rangeOfString' restituirà NSRange, spero che chiarisca il tuo dubbio. –

+1

Non funziona. Provato in Swift 3.0 su iOS 10.2 –

5

Meglio ancora, si può andare avanti e di utilizzare questa funzione 1 nel vostro arsenale, in un file come Constants.swift e quindi è possibile incoraggiare parole all'interno di qualsiasi stringa, in numerose occasioni chiamando solo UNA LINEA del codice:

Per andare in un file senza una classe, come constants.swift nel mio caso:

import Foundation 
import UIKit 

func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString { 
    let nonBoldFontAttribute = [NSFontAttributeName:font!] 
    let boldFontAttribute = [NSFontAttributeName:boldFont!] 
    let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute) 
    boldString.addAttributes(boldFontAttribute, range: fullString.rangeOfString(boldPartOfString as String)) 
    return boldString 
} 

allora si può solo chiamare questa una linea di codice per qualsiasi UILabel:

let normalFont = UIFont(name: "INSERT FONT NAME", size: 14) 
let boldFont = UIFont(name: "INSERT BOLD FONT NAME", size: 14) 
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldSearchFont!) 
+0

So che questo è vecchio, ma c'è un modo per usarlo in grassetto due parti separate della stessa stringa? –

1

Swift 3 Aggiornamento (dalla risposta di @Hamza Ansari)

func attributedText()-> NSAttributedString 
{ 
    let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString 

    let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)]) 

    let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)] 

    // Part of string to be bold 
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE")) 
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:")) 

    // 4 
    return attributedString 
} 
+0

Non funziona. Provato in Swift 3.0 su iOS 10.2 –

Problemi correlati