[英]Set the UITextField alignment to the right
我正在使用iPhone6 +和iOS 8.3。
何时,我想将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];
尝试实现此委托方法:
- (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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.