2015-07-27 17 views
14

Hier ist der Text:fettgedruckter Teil der Zeichenkette in UITextView schnellen

@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) 

i zu fett „Servicebedingungen“ will und „Bitte beachten Sie: Die HIGO Nutzungsbedingungen, wie unten angegeben ist wirksam, wie der ' Zuletzt aktualisiertes 'Datum oben für jeden Benutzer, der die HIGO-Website besucht, oder für jeden Benutzer, der an oder nach diesem Datum ein HIGO-Konto erstellt. "

i mit Verwendung versuchen UILabel programmatisch in UITextView aber nicht funktioniert:

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)" 

auch mit diesen versuchen, aber hat nicht funktioniert, es zeigt nur „Servicebedingungen“ und „last update ..“, tat nicht anzeigen "Bitte beachten ..."

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 

Wie diesen Teil der Zeichenfolge zu fett?

Hinweis: Der Text ist immer noch lang und haben mehr Teil der Zeichenfolge zu fett.

+1

Sie zugeschrieben Zeichenfolge – Miknash

+3

möglich Duplikat verwenden sollten [Making Text fett mit attributierten String in swift] (http://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string-in-swift) – Larme

+0

wie? bitte zeig es mir. – Sarimin

Antwort

30

Aktualisiert für 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 
} 

und Text auf Ihren Text-Ansicht verwenden attributext als:

legalText.attributedText = attributedText() 
+0

Das ist fast korrekt, aber 'rangeOfString' gibt' Range' zurück und 'addAttributes' benötigt einen' NSRange', also musst du damit spielen ein bisschen. Wie auch immer, +1 –

+2

Wenn Sie string als NSString 'rangeOfString' verwenden, wird NSRange zurückgeben, hoffe, dass es Ihre Zweifel löscht. –

+1

Funktioniert nicht. In Swift 3.0 auf iOS 10.2 getestet –

5

noch besser ist, können Sie voran gehen und benutzen Sie diese 1-Funktion in Ihrem Arsenal, in einer Datei wie Constants.swift und dann können Sie Wörter in jeder Zeichenfolge, bei zahlreichen Gelegenheiten durch den Aufruf nur ONE LINE des Codes:

embolden

Um ohne eine Klasse in einer Datei zu gehen, wie constants.swift in meinem Fall:

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 
} 

Dann können Sie ihn nur für jeden UILabel von Code eine Zeile nennen:

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

Ich weiß, das ist alt, aber gibt es eine Möglichkeit, dies zu verwenden, um zwei separate Teile der gleichen Zeichenfolge fett zu machen? –

1

Swift 3 Update (aus der Antwort von @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

Funktioniert nicht. In Swift 3.0 auf iOS 10.2 getestet –

Verwandte Themen