2015-04-28 87 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