如何用程序更改UItext字段占位符和文本到右对齐方式



我正在进行阿拉伯语本地化,其中我必须从右到左进行文本处理。不,我不习惯使用POD或Bundle。我在运行时使用CountryPicker来选择Country。

对于标签,我使用了下面的代码,这是工作

self.userNameLbl.textAlignment = .right

我用过

UITextField.appearance().semanticContentAttribute = .forceRightToLeft

仍然没有使用

我对你的问题有点困惑你想更改文本字段或标签的对齐方式,但对于我使用的文本字段及其工作方式,你必须首先为你的文本过滤创建一个出口,或者在变量中定义它

NameTextField.textAlignment = .right
go to storyboard -> select your textfield -> attributes inspector -> view -> semantic set to forceRightToLeft. 

你可以做:

label.textAlignment = NSTextAlignment.center;

或者,简称:

label.textAlignment = .center;

Swift 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]) 

最新更新