如何以编程方式将UItextfield占位符和文本更改为右对齐

时间:2020-07-06 13:01:26

标签: ios swift uitextfield

我正在对阿拉伯语进行本地化,其中我必须从右向左书写文本。 不,我不会使用POD或Bundle。我在运行时使用CountryPicker选择国家/地区。

对于标签,我确实使用了下面的有效代码

self.userNameLbl.textAlignment = .right

我已经用过

UITextField.appearance().semanticContentAttribute = .forceRightToLeft

还是没用

3 个答案:

答案 0 :(得分:1)

我对您要更改文本字段或标签的对齐方式的问题感到有些困惑,但是对于文本字段,我使用了它,并且它的工作原理是您必须首先为textfild创建出口或将其定义为变量

NameTextField.textAlignment = .right

答案 1 :(得分:0)

您可以这样做:

label.textAlignment = NSTextAlignment.center;

或者,简写为:

label.textAlignment = .center;

雨燕3

label.textAlignment = .center

您可以使用属性字符串设置占位符文本。通过属性传递所需的颜色:

var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                         attributes: [NSForegroundColorAttributeName: UIColor.yellow])

对于Swift 3+,请使用以下命令:

myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                         attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

对于Swift 4.2,请使用以下代码:

myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                         attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]) 

答案 2 :(得分:-1)

go to storyboard -> select your textfield -> attributes inspector -> view -> semantic set to forceRightToLeft.