Question

As a preliminary stage to an app I want to make, I've set up a simple test example of indenting the paragraphs in a UITextView. It basically sets up an NSTextStorage with some text and puts it in a text view. When the user taps an indent button, I want the paragraph(s) that overlap the current selection to indent.

The part that doesn't work is that after the paragraph is indented, the blue selection highlight stays in that location and doesn't move to where the text that was selected is now located. What am I missing to make this refresh happen?

Here's what the working part before the indent looks like:

an image showing the selected text in the text view

And here's what the same selection looks like after the indent:

enter image description here

The code for the indention:

UITextRange *startRange = [textView.tokenizer rangeEnclosingPosition:[textView selectedTextRange].start withGranularity:UITextGranularityParagraph inDirection:UITextStorageDirectionForward];
UITextRange *endRange = [textView.tokenizer rangeEnclosingPosition:[textView selectedTextRange].end withGranularity:UITextGranularityParagraph inDirection:UITextStorageDirectionBackward];
NSInteger startOffset = MIN([textView offsetFromPosition:textView.beginningOfDocument toPosition:startRange.start], [textView offsetFromPosition:textView.beginningOfDocument toPosition:endRange.start]);
NSInteger endOffset = MAX([textView offsetFromPosition:textView.beginningOfDocument toPosition:endRange.end], [textView offsetFromPosition:textView.beginningOfDocument toPosition:startRange.end]);
NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);

[self.textStorage beginEditing];
[self.textStorage enumerateAttribute:NSParagraphStyleAttributeName
                             inRange:offsetRange
                             options:0
                          usingBlock:^(id value, NSRange range, BOOL *stop) {
                              ((NSMutableParagraphStyle *)value).firstLineHeadIndent += [self tabIndent];
                              ((NSMutableParagraphStyle *)value).headIndent += [self tabIndent];
                              [self.textStorage removeAttribute:NSParagraphStyleAttributeName range:range];
                              [self.textStorage addAttribute:NSParagraphStyleAttributeName value:value range:range];
                          }];
[self.textStorage endEditing];

Maybe this is a silly question, but I'd appreciate an outside eye. Thanks for your help!

Was it helpful?

Solution

It is a bit late, but maybe it helps you or others....

You can change the selection through the UITextInput-Protocol by setting the selectedTextRange .

let beginning: UITextPosition = textView.beginningOfDocument
let start: UITextPosition = textView.positionFromPosition(beginning, offset: textView.selectedRange.location)
let end: UITextPosition = textView.positionFromPosition(start!, offset: textView.selectedRange.length)

textView.selectedRange = NSMakeRange(0, 0)
textView.selectedTextRange = textView.textRangeFromPosition(start!, toPosition: end!)

P.S. My code is written in Swift, but I think it is no problem to convert that to objective c, or?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top