3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
浮動(dòng)占位符
將占位符標(biāo)簽放置在文本視圖上方,設(shè)置其字體,顏色并通過(guò)跟蹤文本視圖的字符數(shù)變化來(lái)管理占位符的可見性是簡(jiǎn)單,安全和可靠的。
斯威夫特3:
class NotesViewController : UIViewController, UITextViewDelegate {
@IBOutlet var textView : UITextView!
var placeholderLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
placeholderLabel = UILabel()
placeholderLabel.text = "Enter some text..."
placeholderLabel.font = UIFont.italicSystemFont(ofSize: (textView.font?.pointSize)!)
placeholderLabel.sizeToFit()
textView.addSubview(placeholderLabel)
placeholderLabel.frame.origin = CGPoint(x: 5, y: (textView.font?.pointSize)! / 2)
placeholderLabel.textColor = UIColor.lightGray
placeholderLabel.isHidden = !textView.text.isEmpty
}
func textViewDidChange(_ textView: UITextView) {
placeholderLabel.isHidden = !textView.text.isEmpty
}
}
斯威夫特2:一樣的,除了:italicSystemFontOfSize(textView.font.pointSize),UIColor.lightGrayColor
- 3 回答
- 0 關(guān)注
- 720 瀏覽
添加回答
舉報(bào)