[英]How to set a custom NSTextStorage to a UITextView object created from a storyboard?
I want to set a custom NSTextStorage
to a UITextView
object created from a storyboard.我想将自定义NSTextStorage
设置为从 storyboard 创建的UITextView
object。 If it's really needed I can consider subclassing of UITextView
.如果真的需要,我可以考虑UITextView
的子类化。
(I know if I created a UITextView
object from the code I would be able to use a init(frame: CGRect, textContainer: NSTextContainer?)
constructor.) (我知道如果我从代码中创建了UITextView
object,我将能够使用init(frame: CGRect, textContainer: NSTextContainer?)
构造函数。)
Swift is preferable. Swift 是优选的。
I was able to accomplish this by adding a UITextView
subview to a UIView
wrapper subclass that is instantiated from the storyboard. 我能够通过将UITextView
子视图添加到从故事板实例化的UIView
包装器子类来实现此目的。 You can then add a getter on the view controller that returns the box's textView instance, so that you can assign yourself as the delegate, access properties etc... 然后,您可以在视图控制器上添加一个getter,它返回框的textView实例,以便您可以将自己指定为委托,访问属性等...
Something like 就像是
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet private weak var textViewBox: TextViewBox! // connected in storyboard
var textView: UITextView! {
return textViewBox?.textView
}
override func viewDidLoad() {
super.viewDidLoad()
textView?.delegate = self
}
}
it works for me:这个对我有用:
final class SendTextViewController: BaseViewController {
@IBOutlet private var textView: UITextView!
private var textStorage = MentionTextStorage()
override func viewDidLoad() {
super.viewDidLoad()
textView.textStorage.removeLayoutManager(textView.layoutManager)
textStorage.addLayoutManager(textView.layoutManager)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.