【发布时间】:2019-10-12 19:02:14
【问题描述】:
我想在我的UITextView 上设置垂直对齐。文本视图是可编辑的并且滚动被禁用:
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
我已经尝试子类化文本视图并添加如下属性:
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。
我在互联网上找到的所有其他解决方案也都不起作用,我有点绝望……你能帮帮我吗?
谢谢
【问题讨论】:
标签: ios swift alignment uitextview vertical-alignment