在我的cocoa应用程序(用Swift编写)中,我试图创建一个分页系统,其中每个页面都是CASTextView(NSTextView的子类)的实例。此外,每个CASTextView都保留一个CASTextContainer实例(NSTextContainer的子类)。为了完成分页,我已经使CASTextView的所有实例通过单例模式共享相同的NSLayoutManager实例。
我已经实现了方法- (void)layoutManager:(NSLayoutManager *)aLayoutManager didCompleteLayoutForTextContainer:(NSTextContainer *)aTextContainer atEnd:(BOOL)flag
,如下所示:
func layoutManager(layoutManager: NSLayoutManager!,
didCompleteLayoutForTextContainer textContainer: NSTextContainer!,
atEnd layoutFinishedFlag: Bool)
{
let containers = layoutManager.textContainers
if !layoutFinishedFlag || !textContainer {
let lastContainer = containers[containers.count - 1] as NSTextContainer
if !textContainer || textContainer == lastContainer {
if layoutManager.glyphRangeForTextContainer(lastContainer).length > 0 {
addPage(nil)
}
}
}
}
以下是接收NSLayoutManager(CASTextView.sharedLayoutManager)的单例实例的代码:
class var sharedLayoutManager: NSLayoutManager {
struct Singleton {
static let instance = NSLayoutManager()
}
return Singleton.instance // All text containers share the same layout manager.
}
方法addPage初始化文本视图,用CASTextContainer类型替换文本容器,然后用共享布局管理器替换布局管理器,如下所示:
func addPage(sender: NSNotification?) {
let midpoint = NSWidth(mainView.frame) / 2
let firstEndpoint = midpoint - 500
var rect: NSRect = NSZeroRect
let yValue: CGFloat = 50 + 1425 * CGFloat(pages.count)
mainView.frame.size = NSMakeSize(NSWidth(mainView.frame), yValue + 1675) // extend the scrolling size to account for new subview.
let origin = NSMakePoint(firstEndpoint, yValue)
let size = NSMakeSize(1000, 1375)
rect.origin = origin
rect.size = size
var nextView = CASTextView(frame: rect)
var nextContainer = CASTextContainer(containerSize: containerSize)
nextContainer.mainView = nextView
nextView.replaceTextContainer(nextContainer)
nextContainer.replaceLayoutManager(CASTextView.sharedLayoutManager)
nextView.autoresizingMask = NSAutoresizingMaskOptions.ViewNotSizable
nextView.delegate = nextView
nextView.usesFindPanel = true
mainView.addSubview(nextView)
pages.append(nextView)
for var i = 0; i < pages.count - 1; ++i {
pages[i].nextPage = pages[i + 1] // reset nextPage variable
}
}
每个CASTextView都保留对下一页(也是CASTextView类型)的引用,最后一个CASTextView将此引用(称为nextPage)设置为nil。变量'pages'是由NSLayoutManagerDelegate(NSViewController的子类)保留的所有页面的数组。
问题:对addPage的调用正在执行时(当一个页面的文本容器不再适合所有文本时),但是导致异常发生在以下方法:
-[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {0, 98} out of bounds; string length 0
我无法在任何地方找到有关此方法的文档;前导下划线似乎暗示它是NSString的私有方法,但堆栈跟踪显示它由-[NSString paragraphRangeForRange:]
调用。有趣的是,堆栈跟踪并未显示addPage方法在堆栈上。这是堆栈跟踪:
0 CoreFoundation 0x00007fff90ef3f1c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff869b274e objc_exception_throw + 43
2 CoreFoundation 0x00007fff90ef3dcd +[NSException raise:format:] + 205
3 Foundation 0x00007fff8d480b5c -[NSString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:] + 239
4 Foundation 0x00007fff8d4a699c -[NSString paragraphRangeForRange:] + 57
5 AppKit 0x00007fff8ea8a7fe -[NSTextView _markTextEditedForRange:] + 1405
6 AppKit 0x00007fff8ea852a0 -[NSTextView insertText:replacementRange:] + 2800
7 AppKit 0x00007fff8eaba451 -[NSTextView insertText:] + 318
8 AppKit 0x00007fff8eade292 -[NSTextView(NSKeyBindingCommands) insertNewline:] + 239
9 AppKit 0x00007fff8ea8f544 -[NSTextView doCommandBySelector:] + 195
10 AppKit 0x00007fff8f0b6a71 -[NSTextInputContext(NSInputContext_WithCompletion) doCommandBySelector:completionHandler:] + 118
11 AppKit 0x00007fff8ea64c9b -[NSKeyBindingManager(NSKeyBindingManager_MultiClients) interpretEventAsCommand:forClient:] + 1899
12 AppKit 0x00007fff8f0b54a2 __61-[NSTextInputContext _handleEvent:options:completionHandler:]_block_invoke968 + 335
13 AppKit 0x00007fff8f0b6edf -[NSTextInputContext(NSInputContext_WithCompletion) hasMarkedTextWithCompletionHandler:] + 76
14 AppKit 0x00007fff8f0b52c3 __61-[NSTextInputContext _handleEvent:options:completionHandler:]_block_invoke_3 + 95
15 AppKit 0x00007fff8f0b45d8 -[NSTextInputContext tryHandleEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 101
16 AppKit 0x00007fff8f0b5234 __61-[NSTextInputContext _handleEvent:options:completionHandler:]_block_invoke956 + 321
17 HIToolbox 0x00007fff8e0e204f __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_5 + 70
18 AppKit 0x00007fff8f0ae056 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke174 + 2387
19 AppKit 0x00007fff8f0b6edf -[NSTextInputContext(NSInputContext_WithCompletion) hasMarkedTextWithCompletionHandler:] + 76
20 AppKit 0x00007fff8f0ad674 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2 + 95
21 AppKit 0x00007fff8f0ab3c8 -[NSTextInputContext tryHandleTSMEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 101
22 AppKit 0x00007fff8f0ad3d0 -[NSTextInputContext handleTSMEvent:completionHandler:] + 3293
23 AppKit 0x00007fff8ea8410e _NSTSMEventHandler + 324
24 HIToolbox 0x00007fff8decd6c8 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1128
25 HIToolbox 0x00007fff8deccb8e _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 386
26 HIToolbox 0x00007fff8decca02 SendEventToEventTargetWithOptions + 43
27 HIToolbox 0x00007fff8e0db37c SendTSMEvent_WithCompletionHandler + 417
28 HIToolbox 0x00007fff8e0db87c __SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler_block_invoke + 400
29 HIToolbox 0x00007fff8e0dbb36 __SendFilterTextEvent_WithCompletionHandler_block_invoke + 189
30 HIToolbox 0x00007fff8e0db3d0 SendTSMEvent_WithCompletionHandler + 501
31 HIToolbox 0x00007fff8e0db6bf SendFilterTextEvent_WithCompletionHandler + 236
32 HIToolbox 0x00007fff8e0d8a31 SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler + 295
33 HIToolbox 0x00007fff8e0dd329 __utDeliverTSMEvent_WithCompletionHandler_block_invoke_2 + 296
34 HIToolbox 0x00007fff8e0dd1f8 __utDeliverTSMEvent_WithCompletionHandler_block_invoke + 437
35 HIToolbox 0x00007fff8e0d84ad TSMKeyEvent_WithCompletionHandler + 701
36 HIToolbox 0x00007fff8e0e1fd0 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_4 + 251
37 HIToolbox 0x00007fff8e0e1ec6 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_3 + 330
38 HIToolbox 0x00007fff8e0e1d02 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_2 + 259
39 HIToolbox 0x00007fff8e0e1b85 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke + 251
40 HIToolbox 0x00007fff8e0e198f TSMProcessRawKeyEventWithOptionsAndCompletionHandler + 3068
41 AppKit 0x00007fff8f0b5046 __61-[NSTextInputContext _handleEvent:options:completionHandler:]_block_invoke945 + 131
42 AppKit 0x00007fff8f0b4502 -[NSTextInputContext tryTSMProcessRawKeyEvent:dispatchCondition:setupForDispatch:furtherCondition:dispatchWork:continuation:] + 127
43 AppKit 0x00007fff8f0b4b35 -[NSTextInputContext _handleEvent:options:completionHandler:] + 1266
44 AppKit 0x00007fff8ea8394e -[NSTextInputContext handleEvent:] + 109
45 AppKit 0x00007fff8ea63368 -[NSView interpretKeyEvents:] + 207
46 AppKit 0x00007fff8ea837cd -[NSTextView keyDown:] + 695
47 AppKit 0x00007fff8efa2d7b -[NSWindow _reallySendEvent:] + 1959
48 AppKit 0x00007fff8ea337bc -[NSWindow sendEvent:] + 368
49 AppKit 0x00007fff8e9e5dc0 -[NSApplication sendEvent:] + 4008
50 AppKit 0x00007fff8e871b48 -[NSApplication run] + 711
51 AppKit 0x00007fff8e85d2de NSApplicationMain + 1778
52 Application 0x000000010000e2a5 top_level_code + 37
53 Application 0x000000010000e2da main + 42
54 libdyld.dylib 0x00007fff9422d5c9 start + 1
55 ??? 0x0000000000000003 0x0 + 3
)
答案 0 :(得分:0)
由于我调用了replaceLayoutManager
,错误发生了。我改变了初始化文本视图的方式。我没有使用init(frame: frameRect)
构造函数,而是通过单独初始化文本容器来使用指定的初始化程序:
var nextContainer = CASTextContainer(containerSize: containerSize)
var nextView = CASTextView(frame: rect, textContainer: nextContainer)
然后我继续致电mainView.addSubview(nextView)
,并在添加子视图后 ,我打电话给以下人员:
CASTextView.sharedLayoutManager.addTextContainer(nextContainer)
然后允许发生分页效果。