2017-02-03 6 views
0

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?

Répondre

1

Swift 3 vous pouvez utiliser quelque chose comme ceci:

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 

    } 

} 

l'idée principale que l'utilisation let position = text.distance(from: text.startIndex, to: index) vous avez pas la représentation entière de position de la chaîne, mais la chaîne valeur de l'indice. En utilisant text.distance(from: text.startIndex, to: index) vous pouvez trouver la position int pour la chaîne Index