UITextField Chinese character moves down when editing in iOS 10

I am using Xcode8 and iOS10.

What I did:
In IB, drag UITextFieldinto my main view, add some restrictions to put the text box in the right place, select the text box border style for none, finally enter the text to resize the UITextField in the storyboard, for example: Storyboard TextField

To find out what happens, I also make the text box draw its border by adding this to viewDidLoad: self.textField.layer.borderWidth = 1;

Build and run, it looks good on the device:

enter image description here

Problem

However, if I try to edit the text field (when the text field becomes the first responder), the text moves down:

enter image description here

This problem only occurs when the Textfield frame style is set to none, and the text contains Chinese characters, does anyone know what is happening here?

+4
source share
5 answers

The same question for me. I use UIAlertViewcontain UITextField, I fix it by setting:

 textField.clearButtonMode = UITextFieldViewModeWhileEditing;

This is normal.

Another method: subclass UITextField, rewrite:

- (CGRect)textRectForBounds:(CGRect)bounds {

    return CGRectInset(bounds, 2, 1);

}


- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 2, 1);

}
+3
source

I will fix this problem by setting the borderStyle parameter to nothing but UITextBorderStyleNonein the interface builder, and then in my vc viewDidLoad, change it to UITextBorderStyleNone, I believe this is just one more of the many errors that the interface builder has? :)

+5
source

UITextField. , UITextField. UITextField , , "" .

NSLOG subviews UITextField layoutSubviews func, , UITextEditor contentOffset.

, layoutSuviews func, reset contentOffset.y of UITextEditor [super layoutSubview].

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIScrollView *view in self.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            CGPoint offset = view.contentOffset;
            if (offset.y != 0) {
                offset.y = 0;
                view.contentOffset = offset;
            }
            break;
        }
    }
}
+1

UITextField

1,

  CGSize size = [@"δ½ ε₯½" sizeWithFont:[UIFont systemFontOfSize:14]];

2, textFiled

 CGRect frame = CGRectMake(100, 200, 150, size.height);

 UITextField  textFiled = [[UITextField alloc] initWithFrame:frame];

 [textFiled setFont:[UIFont systemFontOfSize:14]];

!

0
source

We use the storyboard, however we are facing the same problem. After some investigation, we decided to use IBInspectable for hacking. Each UITextField in the storyboard will raise the IBInspectable flag, and the problem will be solved. This is the quick version:

extension UITextField {

  @IBInspectable var hideBorder: Bool {
    get {
      return borderStyle == UITextBorderStyle.none
    }
    set {
      if newValue {
        borderStyle = UITextBorderStyle.line
        borderStyle = UITextBorderStyle.none
      }
    }
  }

}
0
source

Source: https://habr.com/ru/post/1654965/


All Articles