ios – 占位符没有采用NSTextAlignmentNatural

我有一个应用程序,我有UITextField.

我的应用程序是英语 – 阿拉伯语应用

现在我拥有的如下.

addPostVideoURL.backgroundColor = [UIColor clearColor];
addPostVideoURL.textAlignment = NSTextAlignmentNatural; <-- Setting alignment
addPostVideoURL.textColor = pGray_343434;
addPostVideoURL.placeholder = localize(@"videoURL");
[addPostVideoURL setValue:pGray_343434 forKeyPath:@"_placeholderLabel.textColor"];
addPostVideoURL.font = [self adjustDefaultFont:40];
addPostVideoURL.autocorrectionType = UITextAutocorrectionTypeNo;

如果你看到,我已将对齐文本设置为NSTextAlignmentNatural.我这样做是因为如果键盘是阿拉伯语(&我的应用程序是英语或阿拉伯语),我希望textfield从右侧开始.

当我将键盘更改为英语时,文本字段从LTR&如果我将键盘更改为阿拉伯语,则文本字段从RTL开始.但是,我所拥有的占位符始终位于左侧.

有什么方法可以根据应用程序语言进行占位符对齐?

最佳答案 要解决这个问题,下面就是我做的……

>在.pch中,我在下面添加

#define myDirection001([localize(@“myLang”)isEqualToString:@“en”])? NSTextAlignmentLeft:NSTextAlignmentRight
>然后有如下代码.

addPostVideoURL.backgroundColor = [UIColor clearColor];
addPostVideoURL.textAlignment = myDirection001;
addPostVideoURL.textColor = pGray_343434;
addPostVideoURL.placeholder = localize(@"videoURL");
[addPostVideoURL setValue:pGray_343434 forKeyPath:@"_placeholderLabel.textColor"];
addPostVideoURL.font = [self adjustDefaultFont:40];
addPostVideoURL.autocorrectionType = UITextAutocorrectionTypeNo;

确保委托已设置,因为我们现在将调用委托.

>调用委托方法.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.textAlignment = NSTextAlignmentNatural;    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    textField.textAlignment = (textField.text.length==0) ? myDirection001 : NSTextAlignmentNatural;
    return YES;
}
点赞