2017-02-03 1 views
0

Also funktioniert die Methode, die ich in Swift 2 verwendet habe, nicht mehr wegen Änderungen in Swift 3 in Bezug auf String-Indizes und Bereiche. Vorher hatte ichSwift 3: Wie man Attribute auf Teile von Strings anwendet

func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) { 
    if let index = self.text?.characters.indexOf(Character("|")) { 
     self.text = self.text!.stringByReplacingOccurrencesOfString("|", withString: "") 
     let labelLength:Int = Int(String(index))! // Now returns nil 

     var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor] 
     var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor] 
     if boldKeyText { 
      keyAttr[NSFontAttributeName] = UIFont.systemFontOfSize(self.font.pointSize) 
      valAttr[NSFontAttributeName] = UIFont.systemFontOfSize(self.font.pointSize, weight: UIFontWeightHeavy) 
     } 

     let attributeString = NSMutableAttributedString(string: self.text!) 
     attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!)) 
     attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength)) 

     self.attributedText = attributeString 

    } 
} 

Grundsätzlich würde ich eine Zeichenfolge wie nehmen können „Vorname: | Gary Oak“ und haben alle Teile vor und nach dem | Zeichen unterschiedliche Farben, oder machen Sie einen Teil davon fett, aber die Zeile, die ich oben kommentiert hat, gibt keinen Wert mehr zurück, der alles andere danach bricht. Irgendwelche Ideen, wie man das macht?

Antwort

1

In Swift 3 können Sie so etwas wie folgt verwenden:

func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) {  

    if let index = self.text?.characters.index(of: Character("|")) { 
     self.text = self.text!.replacingOccurrences(of: "|", with: "") 
     let position = text.distance(from: text.startIndex, to: index) 

     let labelLength:Int = Int(String(describing: position))! 

     var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor] 
     var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor] 
     if boldKeyText { 
      keyAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize) 
      valAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize, weight: UIFontWeightHeavy) 
     } 

     let attributeString = NSMutableAttributedString(string: self.text!) 
     attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!)) 
     attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength)) 

     self.attributedText = attributeString 

    } 

} 

die Grundidee, dass die Verwendung let position = text.distance(from: text.startIndex, to: index) Sie die Integer-Darstellung von String-Position bekam aber den String-Indexwert. Mit text.distance(from: text.startIndex, to: index) können Sie Int-Position für Zeichenfolge Index

finden
Verwandte Themen