UITextView cursor not positioning properly when ed

2020-02-08 05:45发布

Why is this a thing thats happening

That yellow box above is an editable UITextView that is currently being edited (is first responder). But you probably couldn't tell right? Because there is no cursor. But there is... It's that small blue dot in the bottom left of the yellow textView. It's slightly below the textViews bottom border. So if I keep going to a new line, or pressing enter, the text will move up as it naturally should. But the cursor is never "level", or right above the UITextView's bottom border. It's always just barely poking out of the bottom, a couple points below the border.

Why? This wasn't a problem in iOS 6. Any way to fix this?

5条回答
叛逆
2楼-- · 2020-02-08 05:53

this bug is in iOS 7.0 you can solve this by modifying textView delegate method.

try below code

- (void)textViewDidChange:(UITextView *)textView {
    CGRect line = [textView caretRectForPosition:
        textView.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height
        - ( textView.contentOffset.y + textView.bounds.size.height
        - textView.contentInset.bottom - textView.contentInset.top );
    if ( overflow > 0 ) {
    // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
    // Scroll caret to visible area
        CGPoint offset = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
    // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
}

your problem will solved.

查看更多
狗以群分
3楼-- · 2020-02-08 05:54
- (BOOL) textView:(UITextView *)sender shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString * newText = [sender.text stringByReplacingCharactersInRange:range withString:text];
    [self setFramesForText:newText];

    if (!IOS_VERSION_6_OR_LESS) {
        if ([text isEqualToString:@"\n"]){
            [textView setContentOffset:CGPointMake(textView.contentOffset.x, 999999999)];
        }
    }

    return YES;
}
查看更多
Ridiculous、
4楼-- · 2020-02-08 06:02

You just need to set the tintColor of the UITextView.

Put something like the following in the code (I put it in the viewDidLoad) worked for me.

self.textView.tintColor = [UIColor redColor];
查看更多
小情绪 Triste *
5楼-- · 2020-02-08 06:08

Based on the answer that Todd gave, you could put something like this in a method that runs when the application finishes launching:

   [[UITextView appearance] setTintColor:[UIColor redColor]];
   [[UITextField appearance] setTintColor:[UIColor redColor]];

This will affect all the UITextFields and UITextViews.

查看更多
The star\"
6楼-- · 2020-02-08 06:09

If you're in a navigation controller scenario, I think this is actually a result of a property that was introduced in iOS 7: automaticallyAdjustsScrollViewInsets (see iOS 7 transition guide)

This defaults to YES, and will try to get clever with the UITextView (which is itself a scroll view) by adjusting the scroll offset for the height of the navigation controller.

The solution is therefore to set this property to NO in your viewDidLoad (being careful not to call it on iOS 6 since it's only available as of iOS 7).

查看更多
登录 后发表回答