我想在我的 UITextView
上设置垂直对齐方式。 TextView 可编辑且滚动已禁用:
let textView = UITextView(frame: frame)
textView.backgroundColor = .clear
textView.attributedText = myAttributedString
textView.isUserInteractionEnabled = true
textView.isEditable = true
textView.allowsEditingTextAttributes = true
textView.isScrollEnabled = false
textView.textContainerInset = .zero
self.addSubview(textView)
所以,我想做这样的事情:
textView.verticalAlignment = .center
我已经尝试对 TextView 进行子类化并添加如下属性:
class MyTextView: UITextView {
public var verticalAlignment: UIControl.ContentVerticalAlignment = .center {
didSet {
let topCorrection: CGFloat
switch verticalAlignment {
case .center:
topCorrection = (bounds.size.height - contentSize.height * zoomScale) / 2.0
case .bottom:
topCorrection = (bounds.size.height - contentSize.height * zoomScale)
case .top, .fill:
topCorrection = 0
@unknown default:
topCorrection = 0
}
contentInset.top = max(0, topCorrection)
}
}
}
但它似乎不适用于将 isScrollEnabled
设置为 false
。
我在互联网上找到的所有其他解决方案都不起作用,我有点绝望......你能帮助我吗?
谢谢
最佳答案
也许您打算使用 UITextField?
let field = UITextField()
field.contentVerticalAlignment = UIControlContentVerticalAlignment.center
或者将其设置为.bottom、.top等
关于ios - 可编辑 UITextView 上的垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56322064/