I have a UILabel in which I need to display two different colored strings: Below is my code:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: lbl_question.attributedText];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:52.0f/255.0f green:104.0f/255.0f blue:165.0f/255.0f alpha:1.0f] range: NSMakeRange(0,[result integerValue]+1)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:75.0f/255.0f green:75.0f/255.0f blue:75.0f/255.0f alpha:2.0f] range: NSMakeRange([result integerValue]+1,[strq length])];
[lbl_question setAttributedText: text];
In iOS 6 it works fine but in ios 5 and earlier versions the two strings just got ovelapped everytime. also i want to get width according to text and font. increase height according to their text.
I am sure there has to be solution...Please help me out with this....
You can do this using NSMutableAttributedString. This will work in ios 6,ios 7 and ios 8 also.
Note that NSMakeRange(startin gpoint counting from zero,number of character);
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"RedGreenBlue"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(8,4)];
lable.attributedText=string;
Thanks