简体   繁体   English

Swift中的NSTextStorage子类不会更新UITextView

[英]NSTextStorage subclass in Swift doesn't update UITextView

I'm trying to create a simple NSTextStorage subclass in Swift, but when I type into my textView, no text appears. 我正在尝试在Swift中创建一个简单的NSTextStorage子类,但是当我在textView中键入内容时,没有文本出现。

Here's my setup code in my view controller 这是我的视图控制器中的设置代码

var textView: UITextView!

var textStorage: CustomTextStorage?

override func viewDidLoad() {
    super.viewDidLoad()

    textStorage = CustomTextStorage()

    let layoutManager = NSLayoutManager()
    textStorage!.addLayoutManager(layoutManager)

    let textContainer = NSTextContainer(size: view.bounds.size)
    layoutManager.addTextContainer(textContainer)

    textView = UITextView(frame: view.bounds, textContainer: textContainer)
    view.addSubview(textView)
}

And in my CustomTextStorage subclass: 在我的CustomTextStorage子类中:

var store: NSMutableAttributedString = NSMutableAttributedString()

func string() -> NSString {
    return store.string
}

override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [NSObject : AnyObject] {
    return store.attributesAtIndex(location, effectiveRange: range)
}

override func replaceCharactersInRange(range: NSRange, withString str: String) {
    store.replaceCharactersInRange(range, withString: string)
    edited(NSTextStorageEditActions.EditedCharacters, range: range, changeInLength:countElements(str) - range.length)
}

override func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange) {
    store.setAttributes(attrs, range: range)
    edited(NSTextStorageEditActions.EditedAttributes, range: range, changeInLength: 0)
}

Problem: Every time I type into the textView, replaceCharactersInRange:withString: gets called with the correct string , but a range of {0,0}, and nothing appears in the textView. 问题:每次我键入replaceCharactersInRange:withString:都会使用正确的string调用replaceCharactersInRange:withString:但是stringrange为{0,0},并且replaceCharactersInRange:withString:什么也没有出现。 The suggestions bar above the keyboard does change though. 键盘上方的建议栏确实发生了变化。

As an aside, setAttributes: gets called with a range of {0,1} (the correct value), causing an exception: setAttributes:一句, setAttributes:的调用范围为{0,1}(正确的值),从而导致异常:

NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'

I've avoided this by checking the range is in bounds first, but this doesn't fix the first issue. 我通过首先检查范围是否在范围内来避免了这种情况,但这不能解决第一个问题。

Any help would be fantastic! 任何帮助都太棒了!

It looks like I had this in CustomTextStorage.swift underneath what I showed: 看起来我在显示的内容下面的CustomTextStorage.swift有此内容:

override func processEditing() {
}

Needed to call super.processEditing() within that method. 需要在该方法中调用super.processEditing() Whoops. 哎呀

Leaving this here as I've seen some similar questions on other sites and mailing lists that got no answer. 离开这里,因为我在其他站点和邮件列表上看到了一些类似的问题,但都没有答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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