1

我使用的是 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];
4

1 回答 1

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

看更多

于 2016-03-23T11:16:52.840 回答