我正在 NSTextView[1] 中编辑 HTML 的一个子集,我想模拟一个
标签。
我发现实现它的方法是使用 NSTextAttachment 和自定义 NSTextAttachmentCell,并编写所有代码以插入附件和单元格。问题是,单元格下方有大量空白区域。
这个空间不是单元格本身的一部分——如果我将单元格的整个区域涂成红色,它的大小正好合适,但 TextView 将下一行文本放在红色下方很远的地方。数量似乎取决于单元格上方的文本量;不幸的是,我正在处理长文档,其中
标签至关重要,这会导致应用程序出现重大问题。
这到底是怎么回事?
我的单元子类的货币部分:
- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer
proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position
characterIndex:(NSUInteger)charIndex {
lineFrag.size.width = textContainer.containerSize.width;
lineFrag.size.height = topMargin + TsStyleBaseFontSize *
heightFontSizeMultiplier + bottomMargin;
return lineFrag;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
characterIndex:(NSUInteger)charIndex
layoutManager:(NSLayoutManager *)layoutManager {
NSRect frame = cellFrame;
frame.size.height -= bottomMargin;
frame.size.height -= topMargin;
frame.origin.y += topMargin;
frame.size.width *= widthPercentage;
frame.origin.x += (cellFrame.size.width - frame.size.width)/2;
[color set];
NSRectFill(frame);
}
[1] 我尝试了一个设置了 isEditable 的 WebView,它生成的标记脏得无法使用——特别是,我找不到在
标签中很好地包装文本的方法。
为了回答 Rob Keniger 对插入水平线附件的代码的请求:
- (void)insertHorizontalRule:(id)sender {
NSAttributedString * rule = [TsPage newHorizontalRuleAttributedStringWithStylebook:self.book.stylebook];
NSUInteger loc = self.textView.rangeForUserTextChange.location;
if(loc == NSNotFound) {
NSBeep();
return;
}
if(loc > 0 && [self.textView.textStorage.string characterAtIndex:loc - 1] != '\n') {
NSMutableAttributedString * workspace = rule.mutableCopy;
[workspace.mutableString insertString:@"\n" atIndex:0];
rule = workspace;
}
if([self.textView shouldChangeTextInRange:self.textView.rangeForUserTextChange replacementString:rule.string]) {
[self.textView.textStorage beginEditing];
[self.textView.textStorage replaceCharactersInRange:self.textView.rangeForUserTextChange withAttributedString:rule];
[self.textView.textStorage endEditing];
[self.textView didChangeText];
}
[self.textView scrollRangeToVisible:self.textView.rangeForUserTextChange];
[self reloadPreview:sender];
}
以及TsPage中构造附件字符串的方法:
+ (NSAttributedString *)newHorizontalRuleAttributedStringWithStylebook:(TsStylebook*)stylebook {
TsHorizontalRuleCell * cell = [[TsHorizontalRuleCell alloc] initTextCell:@"—"];
cell.widthPercentage = 0.33;
cell.heightFontSizeMultiplier = 0.25;
cell.topMargin = 12.0;
cell.bottomMargin = 12.0;
cell.color = [NSColor blackColor];
NSTextAttachment * attachment = [[NSTextAttachment alloc] initWithFileWrapper:nil];
attachment.attachmentCell = cell;
cell.attachment = attachment;
NSAttributedString * attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@""];
[str appendAttributedString:attachmentString];
[str.mutableString appendString:@"\n"];
return str;
}
最佳答案
尝试更改单元格类的 cellFrameForTextContainer:proposedLineFragment:glyphPosition:
方法以返回单元格框架原点的 NSZeroPoint
。
(我不知道为什么这会起作用,但提问者和我一直在实时调试它,它确实起作用了。)
发问者补充:看起来,即使返回的矩形被描述为“框架”,它的原点是相对于它所在的行,而不是文档的顶部。因此,如果您希望返回值位于同一行,则返回值的 origin.y
值应设置为零。
(origin.x
值,另一方面,确实 指的是单元格在行中的位置,因此它应该与 lineFrag 相同.origin.x
除非你想改变单元格的水平位置。)
关于objective-c - NSTextAttachmentCell 是一英里高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8558853/