我想在我的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)
}
}
}
但它似乎不适用于设置为false
的isScrollEnabled
。
我在互联网上找到的所有其他解决方案也都不起作用,我对…有点绝望你能帮帮我吗?
谢谢
发布于 2019-05-28 02:42:45
也许你打算使用UITextField?
let field = UITextField()
field.contentVerticalAlignment = UIControlContentVerticalAlignment.center
或将其设置为.bottom、.top等。
发布于 2019-05-27 17:12:38
创建自定义textView
class CustomTextView: UITextView {
override var canBecomeFirstResponder: Bool {
return false
}
override var selectedTextRange: UITextRange? {
get {
return nil
} set {
}
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer,
tapGestureRecognizer.numberOfTapsRequired == 1 {
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
if let longPressGestureRecognizer = gestureRecognizer as? UILongPressGestureRecognizer,
longPressGestureRecognizer.minimumPressDuration < 0.325 {
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
gestureRecognizer.isEnabled = false
return false
}
}
将此文本视图用作文本视图的基类
https://stackoverflow.com/questions/56322064
复制相似问题