简体   繁体   中英

How to get the cursor in an UITextView to reflect an updated typingAttributes for paragraph alignment?

I am using a UITextView to enter some rich text. I have created a button to change the paragraph alignment (left, center, right).

I am applying the alignment to the attributedText when the user selects some text and it works as expected.

However, when the user hit return and is in a new, zero-length paragraph (just after a newline and nothing following), I believe I should change the typingAttributes to reflect the attributes I want the new text to receive.

I used the following code:

if ((paragraphRange.length == 0) && (paragraphRange.location == [mutableText length])) {

    NSMutableParagraphStyle * mutableParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

    [mutableParagraphStyle setAlignment:textAlignment];
    [mutableParagraphStyle setLineBreakMode:NSLineBreakByWordWrapping];

    NSMutableDictionary * attributes = [self.typingAttributes mutableCopy];
    [attributes setObject:mutableParagraphStyle forKey:NSParagraphStyleAttributeName];
    self.typingAttributes = attributes;

It does apply the paragraph alignment once the character is typed, but the cursor in the UITextView does not reflect the change until after the character is typed. I am looking for a mechanism to get the cursor into the right place before the text is typed. For example, if the user selects 'centered', I want the cursor to move to the center of the view to show where the text will do.

Anyone have any ideas on how to do this?

Thanks in advance,

Charlie

Here is how I am doing it and this causes the cursor to move immediately. I think the key is to set the attribute on the whole paragraph. There does seem to be a bug where sometimes the cursor does not move but if you scroll the textview then that causes it to move. Seems to only occur near the top of the uitextview (lines 1 - 5) and if the two styles use the same font size.

There is a similar bug if you select text and change it to or from bold the selected area is not resized to reflect the new width of the changed text. Also if you programatically change the Font to bold for a paragraph then the cursor position is not updated to fit the changed width of the text. However if you also change the text point size the cursor is correctly repositioned for the next text width.

/*! Applies the Normal style to the range returned by [self rangeForUserParagraphAttributeChange]

    @param sender The id of the control sending the message.
 */
- (IBAction) styleNormal:(id)sender
{
    FLOG(@"styleNormal called");
    NSRange charRange = [self rangeForUserParagraphAttributeChange];
    if (charRange.location == NSNotFound) return;
    NSTextStorage *myTextStorage = [self textStorage];

    if ([self isEditable] && charRange.location != NSNotFound)
    {
        [myTextStorage beginEditing];
        [myTextStorage setAttributes:[self normalStyle] range:charRange];
        [myTextStorage endEditing];
    }
    [self setTypingAttributes:[self normalStyle]];
}
- (NSRange)rangeForUserParagraphAttributeChange {
    NSRange paragaphRange = [self.textStorage.string paragraphRangeForRange: self.selectedRange];
    return paragaphRange;
}

For those tracking this, it looks like this is a bug in iOS7. The renderer does not appear to take into account the state of the typingAttributes paragraph alignment when computing the location of the cursor.

Looks like this one needs a bug report to Apple ;-)

Did you try resigning the textView as first responder, then making it first responder right after that.

[textView resignFirstResponder];
[textView becomeFirstResponder];

调用[textView setNeedsLayout]似乎可以修复光标位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM