Ma première question était de savoir comment changer la police d'un exemple de mot "test" dans un textView, et il a été répondu correctement par @bkrl et @Torongo.Modifier le style d'un tableau de String dans un TextView - swift - EDIT
func changeAllOccurence(of string: String, font: UIFont) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: self)
var range = NSMakeRange(0, attributedString.mutableString.length)
while(range.location != NSNotFound)
{
range = attributedString.mutableString.range(of: string, options: .caseInsensitive, range: range)
if(range.location != NSNotFound)
{
attributedString.addAttribute(NSFontAttributeName,
value: font,
range: range)
range = NSMakeRange(range.location + range.length, self.characters.count - (range.location + range.length));
}
}
return attributedString
}
Comme je ne suis toujours pas au courant du code ci-dessus, j'ai essayé d'ajouter quelques lignes afin de généraliser le code afin qu'il puisse fonctionne pour un tableau de chaînes et non une seule chaîne. Mais pour que cela n'a pas été travaux sont la cause a changé la police du dernier mot que ce qui est un motif raisonnable les modifications finales seront le dernier mot qui est « usage »: quelqu'un
let words = ["example", "usage"]
for word in words {
let attributedText = text.changeAllOccurence(of: word, font: UIFont.boldSystemFont(ofSize: 17))
textview.attributedText = attributedText
}
peut vous conseiller comment améliorer le code fourni par @Toromgo afin de travailler pour n'importe quel tableau de chaînes au lieu d'un seul?
Désolé, mais vous devez utiliser [AttributedStrings] (https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html) –