私が書き込もうとしている問題は、すでに2つを使用して解決されていUILabel
ます.iは、最適化の方法を探しています。別の色で(example : hello world )
表示する必要がある文字列があります。UILabel
"hello in red color " and "world in green color"
前もって感謝します
私が書き込もうとしている問題は、すでに2つを使用して解決されていUILabel
ます.iは、最適化の方法を探しています。別の色で(example : hello world )
表示する必要がある文字列があります。UILabel
"hello in red color " and "world in green color"
前もって感謝します
ColoredLabelという名前のテキストラベルがあり、helloを赤で、worldを緑で書きたいとします。
iOS 6から、属性付き文字列を作成し、ラベルの属性付きテキストとして設定できます。
NSMutableAttributedString* str=[[NSMutableAttributedString alloc]initWithString: @"Hello world"];
[str setAttributes: @{ NSForegroundColorAttributeName : [UIColor redColor] } range: NSMakeRange(0, 5) ];
[str setAttributes: @{ NSForegroundColorAttributeName : [UIColor greenColor] } range: NSMakeRange(6, 5) ];
coloredLabel.attributedText= str;
iOS 6 の時点でUILabel
、attributedText
プロパティがあります。NSAttributedString
このプロパティに必要なコンテンツとフォーマット属性を適切に割り当てれば、完了です。これを行う最も簡単な方法は、Interface Builder で、ラベルのテキストを "Plain" から "Attributed" に切り替えるだけです。これを行った後、IB でフォーマット属性を直接設定できます。
(前述のとおり、このソリューションは iOS 6.0 以降でのみ機能します)
iOS 4 との下位互換性を探している場合はNIAttributedLabel
、Nimbus から使用できます。
http://nimbuskit.info/
https://github.com/mattt/TTTAttributedLabelは、iOS 6 未満をターゲットにする必要がある場合の方法です。
はい、次のような画像パターンで色を設定することもできます..
UIColor *LableColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"anyImageName"]];