A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.
Questions tagged [nsrange]
343 questions
291
votes
16 answers
NSRange to Range
How can I convert NSRange to Range in Swift?
I want to use the following UITextFieldDelegate method:
func textField(textField: UITextField!,
shouldChangeCharactersInRange range: NSRange,
replacementString string:…

János
- 32,867
- 38
- 193
- 353
216
votes
13 answers
NSRange from Swift Range?
Problem: NSAttributedString takes an NSRange while I'm using a Swift String that uses Range
let text = "Long paragraph saying something goes here!"
let textRange = text.startIndex..

Jay
- 4,480
- 3
- 25
- 22
95
votes
10 answers
How shouldChangeCharactersInRange works in Swift?
I'm using shouldChangeCharactersInRange as a way of using on-the-fly type search.
However I'm having a problem, shouldChangeCharactersInRange gets called before the text field actually updates:
In Objective C, I solved this using using…

Fudgey
- 3,793
- 7
- 32
- 53
72
votes
6 answers
Shortcut to generate an NSRange for entire length of NSString?
Is there a short way to say "entire string" rather than typing out:
NSMakeRange(0, myString.length)]
It seems silly that the longest part of this kind of code is the least important (because I usually want to search/replace within entire…

Basil Bourque
- 303,325
- 100
- 852
- 1,154
63
votes
9 answers
Make part of a UILabel bold in Swift
I have a UILabel I've made programmatically as:
var label = UILabel()
I've then declared some styling for the label, including a font, such as:
label.frame = CGRect(x: 20, y: myHeaderView.frame.height / 2, width: 300, height: 30)
label.font =…

Nick89
- 2,948
- 10
- 31
- 50
59
votes
4 answers
How do I store an NSRange in a NSMutableArray or other container?
Here's what I want to do:
NSRange r = NSMakeRange(0,5);
id a = [NSMutableArray a];
[a addObject: r]; // but NSRange is not a NSObject *
With a boolean, I'd use code like this:
[a addObject: [NSNumber numberWithBool: YES]];
or with an integer:
[a…

Steven Fisher
- 44,462
- 20
- 138
- 192
54
votes
7 answers
Create UITextRange from NSRange
I need to find the pixel-frame for different ranges in a textview. I'm using the - (CGRect)firstRectForRange:(UITextRange *)range; to do it. However I can't find out how to actually create a UITextRange.
Basically this is what I'm looking for:
-…

Johannes Lund
- 1,897
- 2
- 19
- 32
38
votes
0 answers
NSRangeException Out of Bounds error
I am setting the attributed text of a label, and I am getting this strange error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'. I have never seen…

Chandler De Angelis
- 2,646
- 6
- 32
- 45
35
votes
3 answers
How to capture last 4 characters from NSString
I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string.
I have looked through NSString Class Reference library and the only…

C.Johns
- 10,185
- 20
- 102
- 156
32
votes
4 answers
Convert selectedTextRange UITextRange to NSRange
How can I convert a UITextRange object to an NSRange? I've seen plenty of SO posts about going the other direction but that's the opposite of what I need. I'm using the UITextRange selectedTextRange which is a property of a UITextView. It returns a…

Milo
- 5,041
- 7
- 33
- 59
30
votes
2 answers
Extracting a string with substringWithRange: gives "index out of bounds"
When I try to extract a string from a larger string it gives me a range or index out of bounds error. I might be overlooking something really obvious here. Thanks.
NSString *title = [TBXML textForElement:title1];
TBXMLElement * description1 = [TBXML…

Ray Y
- 1,261
- 3
- 16
- 24
30
votes
2 answers
Difference between NSRange and NSMakeRange
Is there any difference between:
NSRange(location: 0, length: 5)
and:
NSMakeRange(0, 5)
Because Swiftlint throws a warning when I use NSMakeRange, but I don't know why.
Thanks for the Help :-)

Auryn
- 1,117
- 1
- 13
- 37
26
votes
2 answers
How to print a NSRange in NSLog
Say my range is created as
NSRange myRange = {0,100};
How do I print myRange in NSLog? The following is not working
NSLog(@"my range is %@",myRange);

Katedral Pillon
- 14,534
- 25
- 99
- 199
22
votes
1 answer
Getting index of a character in NSString with offset & using substring in Objective-C
I have a string!
NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
What I want to do is:
Assuming the first character in the string is at index 0. Go to the 11th character (That is 'l' in the above case), and find the…

Umair Khan Jadoon
- 2,874
- 11
- 42
- 63
22
votes
1 answer
NSAttributedString and emojis: issue with positions and lengths
I'm coloring some parts of a text coming from an API (think "@mention" as on Twitter) using NSAttributedString.
The API gives me the text and an array of entities representing the parts of the text that are mentions (or links, tags, etc) which…

Eric Aya
- 69,473
- 35
- 181
- 253