[英]Add a UILabel as a subview of UITextView with line breaks and no horizontal indicator
My custom subclass AttributedTextView
of UITextView
: 我的自定义子类
AttributedTextView
的UITextView
:
@IBDesignable class AttributedTextView: UITextView {
private let placeholderLabel = UILabel(frame: .zero)
override func awakeFromNib() {
super.awakeFromNib()
setupPlaceholderLabelIfNeeded()
}
private func setupPlaceholderLabelIfNeeded() {
if placeholderLabel.superview == nil {
placeholderLabel.font = UIFont.openSansLight(withSize: 21)
placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
placeholderLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
placeholderLabel.textColor = UIColor.lightGray
placeholderLabel.text = placeholder.localized
placeholderLabel.numberOfLines = 0
let leading = NSLayoutConstraint(item: placeholderLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
let trailing = NSLayoutConstraint(item: placeholderLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)
let top = NSLayoutConstraint(item: placeholderLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: placeholderLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
addSubview(placeholderLabel)
addConstraints([leading, trailing, top, bottom])
}
}
}
The result is following: 结果如下:
but I would like to achieve the following: 但我想实现以下目标:
How? 怎么样?
请尝试将UILabel
的框架设置为与UITextView
的框架相同。
placeholderLabel.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.