UITextView不显示文本

用户名

我创建了一个UITextView类,并添加了UITextView触摸功能。UITextView出现,但是设定文本不显示,当我触摸UITextView键盘会出现,但是当我开始写的文字,它不会出现。

这是我的UITextView课:

import UIKit

class TextAnnotation: UITextView {

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        self.text = "Text Field"
        self.textColor = UIColor.blackColor()
        self.userInteractionEnabled = true
        self.editable = true
        self.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: 200, height: 50)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

这是我如何调用此类以添加UITextView的方式:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let textCenter = touches.first!.locationInView(view)
    let textView = TextAnnotation(frame: CGRect(x: textCenter.x, y: textCenter.y, width: 200, height: 50), textContainer: NSTextContainer())

    self.view.addSubview(textView)
}

我如何显示设置的文本,为什么在我开始输入时不显示

叶夫根尼·卡坎(Evgeny Karkan)

好了,touchesBegan函数-在初始化子视图并将其添加到超级视图的位置不好,因为它可以被多次调用。
我猜您有个以上的实例textView,并且每个新实例都覆盖了前一个实例。

我可以建议-将textView初始化和布局代码替换init或其他viewDidLoad功能,隐藏您的textView触摸事件,仅显示它即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章