[英]UITextField Right Alignment iOS 7
我有一个问题,当在iOS7上右对齐UITextField时,当用户键入“Space”时,它不会马上出现。 如果我输入另一个字符,则会出现空格。
在iOS 6中确实没有发生
有人知道怎么修这个东西吗?
-(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:@" "];
FROM此stackoverflow答案 - 右对齐的UITextField空格键不会在iOS 7中前进光标
我不知道如何解决它,但我有一个建议,你可以,同时,用一个非常相似的unicode字符(如U + 00A0)替换所有的空格,然后在另一个字符后再切换回来类型?
只需在.h中包含<UITextFieldDelegate>
,设置UITextField.delegate = self;
在viewDidLoad中
然后执行这样的事情:
//!!!!!*****!*!*!!!*!*** 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 :)谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.