我创建了一个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)
}
我如何显示设置的文本,为什么在我开始输入时不显示
好了,touchesBegan
函数-在初始化子视图并将其添加到超级视图的位置不好,因为它可以被多次调用。
我猜您有个以上的实例textView
,并且每个新实例都覆盖了前一个实例。
我可以建议-将textView
初始化和布局代码替换为init
或其他viewDidLoad
功能,隐藏您的textView
触摸事件,仅显示它即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句