简体   繁体   English

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

[英]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.

相关问题 iOS:UITextView与自定义NSTextStorage崩溃 - iOS: UITextView with custom NSTextStorage crashes 如何使用自定义NSTextStorage清除UITextView? [仅限iOS7] - How to clear a UITextView with a custom NSTextStorage? [iOS7 only] 将自定义 NSLayoutManager 添加到在 Storyboard 中创建的 UItextView - Adding Custom NSLayoutManager to UItextView that is created in Storyboard 如何读取在情节提要中创建的xcode UITextView的x,y位置 - how to read the x,y position of xcode UITextView created in storyboard 如何使用创建的自定义IBDesignable类从情节提要中更改按钮的cornerradius - How to change the cornerRadius of button from storyboard with custom IBDesignable class created 如何使用 Swift 中的自动生成密钥从 storyboard 中设置的 UITextView 中检索文本? - How to retrieve text from UITextView set in storyboard using auto generated key in Swift? 如何通过AppDelegate在初始视图控制器(在Storyboard中创建)上设置属性? - How to set properties on initial view controller (created in Storyboard) from AppDelegate? 如何从情节提要中将UITableViewCell添加到以编程方式创建的UITableView对象? - How to add UITableViewCell from storyboard to programmatically created UITableView object? 如何设置在 NSTextStorage 中输入的 *next* 字符的属性 - How to set attributes of the *next* character that is typed in NSTextStorage 如何从框架中将UIView子类设置为故事板自定义类? - How to set an UIView subclass from a framework as a storyboard custom class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM