How to Initialize NSTextStorage with a String in Swift

后端 未结 1 1924
抹茶落季
抹茶落季 2020-12-19 10:51

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 <

相关标签:
1条回答
  • 2020-12-19 11:24

    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.

    From Apple's documents:

    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

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

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

    0 讨论(0)
提交回复
热议问题