在我的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中,当我点击空格键时,光标不会前进.然而,当我在"世界"中输入"w"时,它显示了空间和w.

在iOS 7中,当空格键被点击时,如何推进光标?

更新:

如果我更改文本字段.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协议方法,因此在您的示例中,上述方法将位于[textField setDelegate:self]指定的viewcontroller中.

如果你想找回你的常规空格,你显然还需要记住,当你从文本字段中取出字符串时,用@" "替换@"\u00a0"的出现,从而把文本转换回来.

Objective-c相关问答推荐

导航回上一个视图控制器

Objective c 判断文本字段是否为空

在 iOS 7 上,如果最终包含在选项卡栏控制器中,则使用工具栏推送控制器会留下不可用空间的间隙

如何通过仅更改高度而不更改宽度来调整 UILabel 的大小?

将未知数量的行添加到静态单元UITableView

是否可以 suppress Xcode 4 静态分析器警告?

pathForResource 返回 null

iOS performSelectorOnMainThread 有多个参数

如何为代码自动格式化设置 Xcode 插件

在 NSData 和 base64 字符串之间转换

在运行时判断 iOS 版本?

类在两者中都实现.将使用两者之一

反转 NSString 文本

如何在 Objective-C (iOS) 中判断 NaN 值

使用单独的委托/数据源时的 UITableView 问题

单例释放方法产生警告?

协议和代表之间的区别?

如何判断 NSString 的最后一个字符

更改导航栏的字体

Xcode 在调试时判断表达式