3 回答
TA贡献1804条经验 获得超8个赞
浮动占位符
将占位符标签放置在文本视图上方,设置其字体,颜色并通过跟踪文本视图的字符数变化来管理占位符的可见性是简单,安全和可靠的。
斯威夫特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 关注
- 691 浏览
添加回答
举报