gpt4 book ai didi

ios - 右对齐的 UITextField 空格键在 iOS 7 中不前进光标

转载 作者:IT老高 更新时间:2023-10-28 11:43:51 30 4
gpt4 key购买 nike

在我的 iPad 应用中,我注意到 iOS 6 和 iOS 7 之间的 UITextFields 行为不同。

我创建 UITextField 如下:

UIButton *theButton = (UIButton*)sender;
UITextField *textField = [[UITextField alloc] initWithFrame:[theButton frame]];

[textField setDelegate:self];
[textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[textField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

textField.textAlignment = UITextAlignmentRight;
textField.keyboardType = UIKeyboardTypeDefault;

...

[textField becomeFirstResponder];

在 iOS 6 中,当我键入“hello world”时,当我在“hello”之后点击空格键时,光标会前进一个空格。

在 iOS 7 中,当我按下空格键时光标不会前进。但是,当我在“world”中键入“w”时,它会显示空格和 w。

如何在 iOS 7 中按下空格键时使光标前进?

更新:

如果我将 textField.textAlignment 更改为 UITextAlignmentLeft,那么 iOS 7 中会出现空格。如果可能的话,我希望它保持右对齐。

最佳答案

这有点小题大做,但如果你真的需要它来查看 iOS6 的方式,你可以用 non-breaking space 替换空格。正如它所写的那样。它被区别对待。示例代码可能如下所示:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// only when adding on the end of textfield && it's a space
if (range.location == textField.text.length && [string isEqualToString:@" "]) {
// ignore replacement string and add your own
textField.text = [textField.text stringByAppendingString:@"\u00a0"];
return NO;
}
// for all other cases, proceed with replacement
return YES;
}

如果不清楚, textField:shouldChangeCharactersInRange:replacementString: 是一个 UITextFieldDelegate 协议(protocol)方法,所以在你的例子中,上述方法将在指定的 View Controller 中[textField setDelegate:self].

如果您想恢复常规空格,显然还需要记住通过将出现的 @"\u00a0" 替换为 @"" 来将文本转换回来从文本字段中获取字符串时。

关于ios - 右对齐的 UITextField 空格键在 iOS 7 中不前进光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19569688/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com