質問

I want to change the color of a particular word from a sentence in IOS 5. Let my string be NSString str = @"Your password is abc"; I want to change the color of password 'abc'. Please help me.

Thanks in advance.

役に立ちましたか?

解決

In IOS 6 you could change the font, textColor, and textAlignment for particular word through attributedText you could find the documentation for this here

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextView_Class/Reference/UITextView.html

Same question is answered in stackoverflow the link of the question is as follows

Bold & Non-Bold Text In A Single UILabel?

他のヒント

I think you are looking for this thing, hope it will work for you.

For a string like firstsecondthid, you can do like this. With first written in RED, second in GREEN and third in BLUE.

Example code:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];  
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];  
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];  
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];  

You can use RTLabel just set attributed text in the rtLBLObj.text it will show you relevent output.Just download and drag RTLabel.h and .m file in your project. you can down it from RTLabel

As the previous answers have mentioned, you need to use an NSAttributedString to store formatted text.

However, native support in iOS for attributed strings in UILabels was only introduced in iOS 6.

If you want to support iOS 5 and below, there are many libraries around for displaying an attributed string. I would recommend using TTTAttributedLabel.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top