How to use NSRange [duplicate]
Asked Answered
N

1

8

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

Northumberland answered 29/12, 2015 at 17:53 Comment(2)
nshipster.com/nsrangeKantar
⌥-click on the symbol to see its declaration. location is 0-based.Thermosiphon
P
15

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

Photoactive answered 29/12, 2015 at 17:55 Comment(1)
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.Junction

© 2022 - 2024 — McMap. All rights reserved.