ios - 可编辑 UITextView 上的垂直对齐

标签 ios swift alignment uitextview vertical-alignment

我想在我的 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/

相关文章:

css - 向左而不是向右溢出文本

ios - Swift UNUsernotification applicationIconBadgeNumber 始终显示一个

iOS 二传手 setter/getter

ios - 如何生成视频URL flv格式的缩略图?

html - ABSMIDDLE 在 Firefox 和 Chrome 上的工作方式不同?

html - CSS |匹配行内的动态 div 高度

ios - 如何在 Carthage 框架中使用框架?

ios - 如何在 HStack 中始终位于中心的 Spacers 之间创建 View ?

ios - 为什么使用自动布局时 View 的框架宽度始终为 600 x 600

ios - 如何在 Swift 中获取像 monthSymbols 这样的年份字符串数组?