objective-c – NSTextAttachmentCell是一英里高

objective-c – NSTextAttachmentCell是一英里高,第1张

概述我正在编辑NSTextView [1]中的 HTML子集,我想模拟一个< hr>标签. 我已经弄清楚这样做的方法是使用NSTextAttachment和自定义的NSTextAttachmentCell,并编写所有代码来插入附件和单元格.问题是,单元格下面有大量的空白区域. 这个空间不是单元格本身的一部分 – 如果我将单元格的整个区域绘制成红色,它的大小正确,但是文本视图将下一行文本放在红色下方.数 我正在编辑NSTextVIEw [1]中的 HTML子集,我想模拟一个< hr>标签.

我已经弄清楚这样做的方法是使用NSTextAttachment和自定义的NSTextAttachmentCell,并编写所有代码来插入附件和单元格.问题是,单元格下面有大量的空白区域.

这个空间不是单元格本身的一部分 – 如果我将单元格的整个区域绘制成红色,它的大小正确,但是文本视图将下一行文本放在红色下方.数量似乎取决于单元格上方的文本数量;不幸的是,我正在使用长文件,其中< hr>标签是至关重要的,这会导致应用程序出现重大问题.

到底他妈发生了什么?

我的细胞亚类的钱部分:

- (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,并且它生成的标记非常脏 – 特别是,我找不到一种方法在< p>中很好地包装文本.标签.

要回答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作为单元框架的原点.

(我不知道为什么这应该有效,但提问者和我一直在实时调试它,实际上它确实如此.)

提问者添加:看起来,即使返回的rect被描述为“框架”,它的原点是相对于它所在的行,而不是文档的顶部.因此,如果要将返回值的origin.y值放在同一行上,则应将其设置为零.

(另一方面,origin.x值确实指的是单元格在行中的位置,因此它应该与lineFrag.origin.x相同,除非您想要更改单元格的水平位置.)

总结

以上是内存溢出为你收集整理的objective-c – NSTextAttachmentCell是一英里高全部内容,希望文章能够帮你解决objective-c – NSTextAttachmentCell是一英里高所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1017261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存