NSTextView我試圖根據文本屬性在行旁邊顯示一些額外的符號。
我已成功子類NSLayoutManager化,但布局管理器似乎無法在textContainerInset.
因為我的文本視圖可能有很長的字串,所以我希望保持繪圖與顯示字形的連接。有沒有辦法欺騙布局管理器以便能夠在內容插圖內繪制 - 或者我是否使用另一種方法來代替drawGlyphsForGlyphRange?
我試過super在繪圖前后呼叫,以及存盤和不存盤圖形狀態。我也嘗試setDrawsOutsideLineFragment:YES了字形,但沒有運氣。
Xcode 編輯器本身使用更改標記,所以我知道這在某種程度上是可行的,但很可能我從錯誤的地方尋找。
我的繪圖方法,簡化:
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin {
[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
NSTextStorage *textStorage = self.textStorage;
NSTextContainer *textContainer = self.textContainers[0];
NSRange glyphRange = glyphsToShow;
NSSize offset = self.textContainers.firstObject.textView.textContainerInset;
while (glyphRange.length > 0) {
NSRange charRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL], attributeCharRange, attributeGlyphRange;
id attribute = [textStorage attribute:@"Revision" atIndex:charRange.location longestEffectiveRange:&attributeCharRange inRange:charRange];
attributeGlyphRange = [self glyphRangeForCharacterRange:attributeCharRange actualCharacterRange:NULL];
attributeGlyphRange = NSIntersectionRange(attributeGlyphRange, glyphRange);
if (attribute != nil) {
[NSGraphicsContext saveGraphicsState];
NSRect boundingRect = [self boundingRectForGlyphRange:attributeGlyphRange
inTextContainer:textContainer];
// Find the top of the revision
NSPoint point = NSMakePoint(offset.width - 20, offset.height boundingRect.origin.y 1.0);
NSString *marker = @"*";
[marker drawAtPoint:point withAttributes:@{
NSForegroundColorAttributeName: NSColor.blackColor;
}];
[NSGraphicsContext restoreGraphicsState];
}
glyphRange.length = NSMaxRange(glyphRange) - NSMaxRange(attributeGlyphRange);
glyphRange.location = NSMaxRange(attributeGlyphRange);
}
}
uj5u.com熱心網友回復:
答案比我預想的要簡單得多。
您可以設定lineFragmentPadding關聯NSTextContainer以在邊距中為繪圖騰出更多空間。在為文本容器設定插圖時必須考慮到這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463360.html
標籤:目标-c 可可 应用套件 nstextview nslayoutmanager
