简体   繁体   English

UITextField切断了文本的右侧,并且没有在正确的位置显示光标

[英]UITextField is cutting off the right side of text and not displaying the cursor at the correct position

This problem can be demonstrated by creating a new project in Xcode (I am using version 6.4) and using the following code: 通过在Xcode中创建一个新项目(我正在使用6.4版)并使用以下代码,可以证明此问题:

@interface ViewController ()
@property (nonatomic, strong) UITextField * myTextField;
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width-100, 50)];
    self.myTextField.textAlignment = NSTextAlignmentRight;
    [self.myTextField becomeFirstResponder];
    self.myTextField.backgroundColor = [UIColor lightGrayColor]
    self.myTextField.adjustsFontSizeToFitWidth = YES;
    self.myTextField.font = [UIFont systemFontOfSize:40];
    [self.view addSubview:self.myTextField];
}

- (void) viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    self.myTextField.text = @"1.5241578750119E18";
}

When running this project in iOS Simulator (iPhone 5 or 5S), the cursor is initially displayed after the last "1", and the "8" is not visible until a new character is typed. 在iOS Simulator(iPhone 5或5S)中运行此项目时,光标最初显示在最后一个“ 1”之后,并且在键入新字符之前“ 8”不可见。

This appears to be a bug by Apple, but my question is: is there a workaround for now that will force the text to right-align and show the cursor in the correct position? 这似乎是Apple的错误,但我的问题是:现在是否有变通办法,将迫使文本右对齐并在正确的位置显示光标?

To clarify the question further, the issue occurs when the text is set programmatically. 为了进一步阐明问题,以编程方式设置文本时会发生此问题。 I expect to see this: 我希望看到:

请注意,在此图片中显示了完整的数字(最后一个“ 8”可见)

But instead I am seeing this (note that the entire number is not visible and the cursor is showing after the "1" instead of the last digit which is an "8"): 但是我看到了这一点(请注意,整个数字不可见,并且光标显示在“ 1”之后,而不是最后一个数字“ 8”之后):

请注意,在此图片中,最后的“ 8”不可见。

This is a bug, existing in iOS, since iOS 7. Issue can be reproduced in stock applications like Settings as well. 这是自iOS 7以来在iOS中存在的错误。也可以在“设置”之类的普通应用程序中复制该问题。 It affects text fields only when NSTextAlignmentRight is used. 仅当使用NSTextAlignmentRight时,它才会影响文本字段。 The original bug ID logged into Radar for this issue is 14485694 . 为此问题登录到Radar的原始错误ID是14485694 You may use centre or left text alignments, to circumvent this problem. 您可以使用居中或左对齐方式来避免此问题。

I would also suggest to file a new bug report to Apple, 我还建议向Apple提交新的错误报告,

Try this 尝试这个

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
self.myTextField.leftView = paddingView;
self.myTextField.leftViewMode = UITextFieldViewModeAlways;

it will add some space to the left side of your TextField so that your cursor starts at correct position. 它将在TextField的左侧添加一些空间,以便光标从正确的位置开始。

Hope it helps. 希望能帮助到你。

If I change your code to this: 如果我将您的代码更改为此:

- (void)viewDidLoad {

    [super viewDidLoad];
        self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 50, self.view.frame.size.width-10, 50)];
    self.myTextField.textAlignment = NSTextAlignmentRight;
    [self.myTextField becomeFirstResponder];
    self.myTextField.backgroundColor = [UIColor lightGrayColor];
    self.myTextField.adjustsFontSizeToFitWidth = YES;
    self.myTextField.font = [UIFont systemFontOfSize:60];
    [self.view addSubview:self.myTextField];
}

And start typing in those numbers, the cursor ends up along the right edge of the text field just as it's supposed to. 然后开始输入这些数字,光标将按照预期的那样沿文本字段的右边缘结束。

Even when I start the app with the default value in the text, I see the cursor along the right edge of the text field fine. 即使使用文本中的默认值启动应用程序,我也可以看到文本字段右侧的光标很好。

UITextFieldTextDidChangeNotification通知而不是UIControlEventEditingChanged可以解决此问题。

I met the same issue, try add dummy code like this 我遇到了同样的问题,请尝试添加这样的虚拟代码

(void)textFieldDidBeginEditing:(UITextField *)textField {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    textField.text = textField.text;
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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