7

I am using NSRange with an Attributed string, but I don't fully understand which parts of the text will be bolded and which are not. This is what I tried:

NSRange boldedRange = NSMakeRange(2, 4);

What exactly does the 2 stand for and the 4 stand for?

Thanks in advance for anyone who contributes to this post

Ameya Vaidya
  • 117
  • 1
  • 9

1 Answers1

13

It says as:

NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>)

So in your case:

NSRange boldedRange = NSMakeRange(2, 4);

2 is the starting location.

4 is the character count till what it will use.

Ex. If you use boldedRange on TYPEWRITER, it will change it to TYPEWRITER

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • With the standard warning: ranges apply to a UTF-16 encoding so oddities may occur with strings that include far-from-Latin characters (emojis being an example you may encounter regardless of language). I can tell you from first-hand experience that putting a colour change within an emoji caused CoreText to hang indefinitely under iOS 4; I doubt that exact problem persists but a mid-glyph colour change doesn't make any sense so something other than your intention would have to happen. – Tommy Dec 29 '15 at 18:52