首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >可编辑UITextView上的垂直对齐方式

可编辑UITextView上的垂直对齐方式
EN

Stack Overflow用户
提问于 2019-05-27 16:09:00
回答 2查看 245关注 0票数 0

我想在我的UITextView上设置垂直对齐。文本视图是可编辑的,并且禁用滚动:

代码语言:javascript
复制
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)

所以,我想做一些类似的事情:

代码语言:javascript
复制
textView.verticalAlignment = .center

我已经尝试了文本视图的子类化,并添加了如下属性:

代码语言:javascript
复制
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)
        }
    }
}

但它似乎不适用于设置为falseisScrollEnabled

我在互联网上找到的所有其他解决方案也都不起作用,我对…有点绝望你能帮帮我吗?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2019-05-28 02:42:45

也许你打算使用UITextField?

代码语言:javascript
复制
let field = UITextField()
field.contentVerticalAlignment = UIControlContentVerticalAlignment.center

或将其设置为.bottom、.top等。

票数 0
EN

Stack Overflow用户

发布于 2019-05-27 17:12:38

创建自定义textView

代码语言:javascript
复制
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
    }
}

将此文本视图用作文本视图的基类

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56322064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档