0

It is possible to change a NSString color by letter?

For example, if you have NSString *A=@"ASDFGH";

show in UILabel ASD with red and FGH with blue.

And another question.. If I have a NSString, can I access the letter I want? For example, in NSString *A=@"ASDFGH"; how can I know what is the second letter which is S, or the third, which is D?

Paul
  • 1

2 Answers2

0

1) You need to either create separate UILabels for each group of characters in the string , use NSAttributedString (see this), or you can just use CoreGraphics to render each group in a different color. The latter is probably the most efficient but also the most complex.

2) Use -[NSString characterAtIndex:] like so:

NSString *string = @"ASDFGH";
unichar char = [string characterAtIndex:1]; //the second letter
if (char == 'S') {
  //do something
}
Community
  • 1
  • 1
Nick C
  • 645
  • 3
  • 6
0

for your second question you can use these three methods of NSString

 – substringFromIndex:
 – substringWithRange:
 – substringToIndex:
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88