UITextField右对齐iOS 7

时间:2013-12-17 16:01:17

标签: ios ios7 uitextfield

我有一个问题,当在iOS7上右对齐UITextField时,当用户键入“Space”时,它不会马上出现。如果我输入另一个字符,则显示空格。

在iOS 6中确实没有发生

http://www.blogosfera.co.uk/2013/10/ios-7-whitespace-not-visible-to-uitextfield-with-right-alignment/

任何人都知道如何解决这个问题?

2 个答案:

答案 0 :(得分:14)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    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;
}

从文本中删除代码

self.txtFirstName.text = [self.txtFirstName.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];

来自此stackoverflow答案 - Right aligned UITextField spacebar does not advance cursor in iOS 7

答案 1 :(得分:0)

我不知道如何解决它,但我有一个建议,你可以,同时,用一个非常相似的unicode字符(如U + 00A0)替换所有空格,然后将它们切换回另一个字符已被键入?

只需在.h中加入<UITextFieldDelegate>,在viewDidLoad中设置UITextField.delegate = self;

然后执行以下内容:

 //!!!!!*****!*!*!!!*!*** Make SURE you set the delegate code before doing this (as mentioned in the original SO answer)

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@" "]) {
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"\u00A0"];//U+00A0 is a unicode character that seems very similar to space but isn't treated as whitespace...
    } else {
        textField.text = [textField.text stringByReplacingOccurrencesOfString:@"\u00A0" withString:@" "];//switches our u+00A0 unicode character back to a white-space everytime a space is not typed.
    }
    return (![string isEqualToString:@" "]);//returns YES if it doesn't equal whitespace, else NO (because we did a manual replace)
}

*注意,我还没有机会测试它,因为我不在xCodeProj附近。让我知道它是如何工作的:)如果有人看到任何错误,请随时编辑O :)谢谢大家!