La méthode que j'ai utilisée dans Swift 2 ne fonctionne donc plus à cause des changements dans Swift 3, en ce qui concerne les indices et les plages de chaînes. Auparavant, j'avaisSwift 3: Comment appliquer des attributs à des parties de chaîne
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
}
}
Fondamentalement, je serais en mesure de prendre une chaîne comme « Prénom: | Gary Oak » et ont toutes les parties avant et après la | être de couleurs différentes, ou en faire une partie en gras, mais la ligne que j'ai commentée ci-dessus ne renvoie plus de valeur, ce qui brise tout le reste par la suite. auriez vous des idées pour faire ça?