繁体   English   中英

如何将自定义 NSTextStorage 设置为从 storyboard 创建的 UITextView object?

[英]How to set a custom NSTextStorage to a UITextView object created from a storyboard?

我想将自定义NSTextStorage设置为从 storyboard 创建的UITextView object。 如果真的需要,我可以考虑UITextView的子类化。

(我知道如果我从代码中创建了UITextView object,我将能够使用init(frame: CGRect, textContainer: NSTextContainer?)构造函数。)

Swift 是优选的。

我能够通过将UITextView子视图添加到从故事板实例化的UIView包装器子类来实现此目的。 然后,您可以在视图控制器上添加一个getter,它返回框的textView实例,以便您可以将自己指定为委托,访问属性等...

就像是

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
    }
}

这个对我有用:

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM