2015-04-28 72 views
1

我正在使用iPhone6 +和iOS 8.3。将UITextField对齐设置为正确

当,我想设置UITextField对齐到右侧。但是当我发送它时,输入文本跳转到左侧。

[tf setBackgroundColor:[UIColor clearColor]]; 
tf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:ISMColor(123, 123, 129)}]; 
tf.delegate = self; 
tf.placeholder = placeholder; 
[tf setTextAlignment:NSTextAlignmentRight]; 
[tf setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]; 
+2

是否有与文本字段您最初定义后相互作用的任何代码?如果是的话,这也值得一提。您是否尝试过其他(模拟)设备而不是6+? – mrkwse

+0

在我的新项目中,它的工作使用相同的代码, –

+0

我得到了同样的问题,你找到了一个解决方案吗? –

回答

0

尝试实现这个委托方法:

- (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; 
} 

See more