UITextPosition to NSRange
Asked Answered
S

4

18

I need to use NSStringFromRange but I only have a start and end UITextPositions. How do you convert UITextPosition to NSRange.

NSMutableArray * wordArray = [[NSMutableArray alloc]init];
id<UITextInputTokenizer> tokenizer = tv.tokenizer;
UITextPosition *start = tv.beginningOfDocument;
while (![start isEqual:tv.endOfDocument]) {
    UITextPosition *end = [tokenizer positionFromPosition:start toBoundary:UITextGranularityWord inDirection:UITextStorageDirectionForward];
    NSString *word = [tv textInRange:[tv textRangeFromPosition:start toPosition:end]];
    [wordArray addObject:NSStringFromRange(<#NSRange range#>)];
    start = end;
}
Sciolism answered 31/7, 2012 at 2:5 Comment(0)
O
24

Try this:

NSInteger startOffset = [tv offsetFromPosition:tv.beginningOfDocument toPosition:start];
NSInteger endOffset = [tv offsetFromPosition:tv.beginningOfDocument toPosition:end];
NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
[wordArray addObject:NSStringFromRange(offsetRange)];
Overpowering answered 31/7, 2012 at 3:40 Comment(1)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextView characterOffsetOfPosition:withinRange:]: unrecognized selector sent to instance 0x71da000'Sciolism
S
8
- (NSRange)rangeFromTextRange:(UITextRange *)textRange inTextView:(UITextView*)textView {

    UITextPosition* beginning = textView.beginningOfDocument;
    UITextPosition* start = textRange.start;
    UITextPosition* end = textRange.end;

    const NSInteger location = [textView offsetFromPosition:beginning toPosition:start];
    const NSInteger length = [textView offsetFromPosition:start toPosition:end];

    return NSMakeRange(location, length);
}
Sheepherder answered 25/1, 2014 at 20:40 Comment(0)
E
5

Swift3 version

    func angeFromTextRange(textRange:UITextRange,textView:UITextView) -> NSRange {
       let location:Int = textView.offset(from: textView.beginningOfDocument, to: textRange.start)
       let length:Int = textView.offset(from: textRange.start, to: textRange.end)
       return NSMakeRange(location, length)
}
Elijaheliminate answered 3/9, 2017 at 16:28 Comment(0)
M
2

Swift 2 version for @Peter Lapisu answer

Method 1: As function

func rangeFromTextRange(textRange:UITextRange,textView:UITextView) -> NSRange {
        let location:Int = textView.offsetFromPosition(textView.beginningOfDocument, toPosition: textRange.start)
        let length:Int = textView.offsetFromPosition(textRange.start, toPosition: textRange.end)
        return NSMakeRange(location, length)
    }

Method 2: As extension

extension UITextView {
    func rangeFromTextRange(textRange:UITextRange) -> NSRange {
        let location:Int = self.offsetFromPosition(self.beginningOfDocument, toPosition: textRange.start)
        let length:Int = self.offsetFromPosition(textRange.start, toPosition: textRange.end)
        return NSMakeRange(location, length)
    }
}
Malissamalissia answered 5/6, 2016 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.