如何在UITextField中突出显示部分文本?

Ara*_*ell 2 uitextfield ios swift

假设我有一个这样的文本:“在注册时,我同意Rue.Du.8的使用条款和隐私政策” UITextField,我希望将除“使用条款”之外的整个颜色设为灰色。有可能实现成UITextField

在此处输入图片说明

提前致谢 ...

Nir*_*v D 5

您可以使用attributedPlaceholder属性来执行此操作,首先创建一个AttributedString,就像这样。

var str = "By signing up I agree to the Terms of use and Privacy policy of Rue.Du.8"
var attributedString = NSMutableAttributedString(string: str)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(), range: (str as NSString).rangeOfString(str))
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: (str as NSString).rangeOfString("Terms of use"))
self.textField.attributedPlaceholder = attributedString 
Run Code Online (Sandbox Code Playgroud)