本文介绍了如何在Swift中使用String初始化NSTextStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In order to break another problem down into smaller parts, I am trying to set up all the TextKit components. However, I am getting an crash after changing how I initialize NSTextStorage. For testing purposes I have simplified the project to the following:

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 崩溃,异常代码为。那段代码不是很解释。基本问题似乎归结为我如何初始化 NSTextStorage

It crashes at enumerateLineFragmentsForGlyphRange with an exception code of EXC_I386_GPFLT. That code isn't very explanitory. The basic problem seems to be coming down to how I am initializing NSTextStorage.

如果我更换

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

这个

layoutManager.textStorage = textView.textStorage

然后它可以工作。我做错了什么?

then it works. What am I doing wrong?

推荐答案

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

It seems the way to do things, is to add the NSLayoutManager to the NSTextStorage object, (using addLayoutManager:) rather than setting the textStorage property on the layout manager.

来自Apple的文档:

From Apple's documents:

大概在'addLayoutManager:'中完成了一些事情,这在setTextStorage中无法完成,导致崩溃。

Presumably something gets done in 'addLayoutManager:' which doesn't get done in setTextStorage, causing the crash.

你可能还想增加textStorage变量的范围,如果它看起来在viewDidLoad完成后被清除了。

You might also want to increase the scope of the textStorage variable, if it appears that it's getting cleared up once viewDidLoad finish.

这篇关于如何在Swift中使用String初始化NSTextStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 02:31