私はNSAttributedString
を生成しようとしていますが、これは基本的にデフォルトのフォントしか持っていない、イタリック体と別のテキスト色です。これまでのところ、十分に簡単です。NSAttributedString with UIFontWeightTraits
ここでは、この文字列全体の異なる部分文字列をに加えて、さらにに太字で表記します。基本的には次のようになります。
ジョンスミス on 25.08私はNSMutableAttributedString
さんaddAttributes(_:_:)
関数に渡すことを、辞書が間違って取得していますように八時
で(ただ、別の色インチ)
に見えます。
[UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]
しかし、これは動作していないよう:documentationから、私はこの辞書は、次のようになりますことを理解しました。私が終わるのは、文字列のイタリック体です。私は明らかにここで何か間違っています。何か案が?
更新:
// Preparation
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
let italicFont = UIFont.italicSystemFontOfSize(14)
// Make entire string italic: works!
attributedString.addAttributes([NSFontAttributeName : italicFont], range: NSMakeRange(0, 33))
// Make the name string additionally bold: doesn't work!
attributedString.addAttributes([UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]], range: nameRange)
// Show it on the label
attributedStringLabel.attributedText = attributedString
簡単な例を追加ありがとう!
あなたは複数の属性の文字列を取得し、表示するために1にそれらを結合する必要があります。 –
上記の関数を使って事実の後に追加することはできないのですか? – flohei