1

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.

AKS
  • 145
  • 2
  • 10

4 Answers4

4

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?

Community
  • 1
  • 1
Rajat
  • 1,043
  • 12
  • 23
1

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)];  
NightFury
  • 13,436
  • 6
  • 71
  • 120
Shahab Qureshi
  • 952
  • 1
  • 7
  • 19
  • I used this code. But i am getting the error message, 'use of undeclared identifier NSForegroundColorAttributeName'. What will i do? – AKS Jun 24 '13 at 08:49
  • Replace this [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)]; with [string addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:NSMakeRange(0,5)]; – Shahab Qureshi Jun 24 '13 at 08:58
  • How can we set this NSMutableAttributedString to a label. I am very sorry if my question is wrong. I am a beginner in IOS. I used the code myLabel.text = (NSString*)string; But i am getting crash. – AKS Jun 24 '13 at 09:15
  • Very simple Just do this self.attributedLabel.attributedText = string; – Shahab Qureshi Jun 24 '13 at 09:17
  • Remember no question is a silly question when you are in learning phase. So what ever is in your mind ask, explore and reach to answers. Try to improve you google skills to. If my answers worked for you, don't forget to mark as answer or VOTE correct. Thanks – Shahab Qureshi Jun 24 '13 at 09:21
  • I am getting an error 'property attributedText not found the object of type UILabel '. – AKS Jun 24 '13 at 09:31
  • Is it support only in versions higher than IOS 5. I am using IOS 5. – AKS Jun 24 '13 at 09:34
  • This works for IOS 6+, which IOS you using btw ? – Shahab Qureshi Jun 24 '13 at 09:35
  • How can we do it in IOS 5? – AKS Jun 24 '13 at 09:49
  • for ios5 Use a CATextLayer see the link provided in the answer http://stackoverflow.com/questions/3586871/bold-non-bold-text-in-a-single-uilabel – Shahab Qureshi Jun 24 '13 at 10:59
  • This link could be very useful, use OHAttributedLabel https://github.com/AliSoftware/OHAttributedLabel – Shahab Qureshi Jun 24 '13 at 11:21
  • Getting error when using OHAttributedLabel. – AKS Jun 24 '13 at 11:34
0

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

Prateek Prem
  • 1,544
  • 11
  • 14
  • ya it is a good method. But how can i append the value of text field to the label using this method. – AKS Jul 01 '13 at 07:25
0

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.

colincameron
  • 2,696
  • 4
  • 23
  • 46