部分的に太字の文字列でラベルのテキストを設定します。私が大胆にしたい言葉は、すべて同じ文字で始まり、「〜」と言います。例えば文字で始まる太字の単語
私は、文字列を持っている可能性があり「これは〜言葉が大胆で、そしてそうである〜この」
そして、ラベルのテキストは、この言葉は大胆で、そしてのでこれは」という文字列が含まれます "となります。
このような機能を実現することができるかどうかは知りませんか?私は、次のことを試してみました:
func makeStringBoldForLabel(str: String) {
var finalStr = ""
let words = str.components(separatedBy: " ")
for var word in words {
if word.characters.first == "~" {
var att = [NSFontAttributeName : boldFont]
let realWord = word.substring(from: word.startIndex)
finalStr = finalStr + NSMutableAttributedString(string:realWord, attributes:att)
} else {
finalStr = finalStr + word
}
}
}
が、エラーが出ます:
Binary operator '+' cannot be applied to operands of type 'String' and 'NSMutableAttributedString'
エラーがライン上で起こる「finalStr = finalStr + NSMutableAttributedString(文字列:realWord、属性:ATT)」と私は私が2 nsmutableattributedstringsを追加することはできませんというエラーを取得しnsmutableattributedstringとして「finalStr」をキャストした場合 –
Ahhh。私は答えを更新しました。これはあなたの問題を解決するはずです。 – Finn
問題はNSMutableAttributedStringsで '+'を使用できないことです。代わりに.append()を使用する必要があります。 – Finn