如何使用 Swift 中的字符串初始化 NSTextStorage

How to Initialize NSTextStorage with a String in Swift

为了将 another problem 分解成更小的部分,我正在尝试设置所有 TextKit 组件。但是,在更改初始化 NSTextStorage 的方式后,我遇到了崩溃。出于测试目的,我已将项目简化为以下内容:

import UIKit

class ViewController3: UIViewController {

    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var myTextView: MyTextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let container = NSTextContainer(size: myTextView.bounds.size)
        let layoutManager = NSLayoutManager()
        let textStorage = NSTextStorage(string: "This is a test")
        layoutManager.addTextContainer(container)

        //layoutManager.textStorage = textView.textStorage  // This works
        layoutManager.textStorage = textStorage  // This doesn't work

        myTextView.layoutManager = layoutManager

    }
}

class MyTextView: UIView {

    var layoutManager: NSLayoutManager?

    override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext();

        // Enumerate all the line fragments in the text
        layoutManager?.enumerateLineFragmentsForGlyphRange(NSMakeRange(0, layoutManager!.numberOfGlyphs), usingBlock: {
            (lineRect: CGRect, usedRect: CGRect, textContainer: NSTextContainer!, glyphRange: NSRange, stop: UnsafeMutablePointer<ObjCBool>) -> Void in

            // Draw the line fragment
            self.layoutManager?.drawGlyphsForGlyphRange(glyphRange, atPoint: CGPointMake(0, 0))

        })
    }
}

它在 enumerateLineFragmentsForGlyphRange 崩溃,异常代码为 EXC_I386_GPFLT。该代码不是很解释。基本问题似乎归结为我如何初始化 NSTextStorage.

如果我替换

let textStorage = NSTextStorage(string: "This is a test")
layoutManager.textStorage = textStorage

有了这个

layoutManager.textStorage = textView.textStorage

然后就可以了。我做错了什么?

看起来做事的方法是将 NSLayoutManager 添加到 NSTextStorage 对象,(使用 addLayoutManager:) 而不是在布局管理器上设置 textStorage 属性。

来自 Apple 的文档:

This method is invoked automatically when you add an NSLayoutManager to an NSTextStorage object; you should never need to invoke it directly, but you might want to override it. If you want to replace the NSTextStorage object for an established group of text-system objects containing the receiver, use replaceTextStorage:.

Link to setTextStorage: for NSLayoutManager

可能在 'addLayoutManager:' 中完成了一些在 setTextStorage 中没有完成的事情,导致了崩溃。

您可能还想增加 textStorage 变量的范围,如果它似乎在 viewDidLoad 完成后被清除。