#14 UITextField PlaceHolder 文字改變顏色
UITextField PlaceHolder
Published in
Aug 5, 2021
text field 的 placeholder 可顯示提示文字,提醒使用者該輸入什麼。所以總是以灰色見人,但是現在每個人都很不容易滿足,PlaceHolder 也是,他也想要有其他的顏色,讓大家可以注意到他。
要更改PlaceHolder 文字的顏色,有2個方法,如下
StoryBoard 寫法
點選UITextField,然後選取 Identity inspector,在user defined runtime attributes 裡面加placeholderLabel.textColor,type 選Color,Value 選你要的顏色,就可以了。
顯示出來就是黑色的PlaceHolder 文字
UIKit 寫法
我們必須使用 text field 的 attributedPlaceholder 屬性。attributedPlaceholder 的型別是 NSAttributedString,因此我們可以完全客製化 placeholder 文字的顏色和字型,
用中間Email address textfield 做例子:
emailAddress.attributedPlaceholder = NSAttributedString(string: " Email address", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
顯示出來就是紅色的PlaceHolder 文字