0

色の異なるテキストを含むラベルを作成したいと考えています。このような、

ここに画像の説明を入力

私はiOS 6でこれを行いましたNSMutableAttributedString

 NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: self.exerciseLbl.attributedText];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:(187/255.0) green:(57/255.0) blue:(38/255.0) alpha:1] range: NSMakeRange(0, 2)];
[self.exerciseLbl setAttributedText: text];

ただし、これは iOS 5.1 以下では機能しません。では、iOS 5 で同じ結果を得るにはどうすればよいでしょうか。

4

3 に答える 3

1
/**(1)** Build the NSAttributedString *******/

NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"Hello World!"];
// for those calls we don't specify a range so it affects the whole string
[attrStr setFont:[UIFont systemFontOfSize:12]];
[attrStr setTextColor:[UIColor grayColor]];
// now we only change the color of "Hello"
 [attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,5)];
/**(2)** Affect the NSAttributedString to the OHAttributedLabel *******/
myAttributedLabel.attributedText = attrStr;`
// Use the "Justified" alignment
`myAttributedLabel.textAlignment = UITextAlignmentJustify;`
于 2013-07-24T06:54:34.227 に答える
0

6 未満の iOS の場合は、 https://github.com/AliSoftware/OHAttributedLabelに進むことができます。それは私のために働いたカテゴリとサブクラスを提供します..あなたにも役立つことを願っています.

于 2013-07-24T06:26:41.440 に答える