私の最初の質問は、textViewの単語の例 "test"のフォントを変更する方法で、@ bkrlと@Torongoによって正しく答えられました。TextView内のString配列のスタイルを変更する - スウィフト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
}
私はまだ上記のコードに慣れていないよ、私はそれが文字列の配列と、1つの文字列だけでなく、ために働くことができるようにコードを一般化するために数行を追加しようとしました。 しかし、それは作品は、それが最終的な変更は、「使用」である最後の言葉のためになり、合理的な原因であるだけで最後の単語のフォントを変更して引き起こさなかったことを確認のために:どのように誰かが助言することができ
let words = ["example", "usage"]
for word in words {
let attributedText = text.changeAllOccurence(of: word, font: UIFont.boldSystemFont(ofSize: 17))
textview.attributedText = attributedText
}
@Toromgoが提供するコードを改善して、ただ1つの文字列ではなく文字列の配列を処理しますか?
申し訳ありませんが、[AttributedStrings](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html)を使用する必要があります –