I have a NSTextField and I want to give the user the opportunity to make invisible characters like blanks, carriage returns and tabs visible. Unfortunately I didn't find a word in Apple's documentaion about this. I assume I'm not using the right term when looking for it.
我有一个NSTextField,我想让用户有机会制作不可见的字符,如空格,回车和标签可见。不幸的是,我没有在Apple的文档中找到关于此的话。我想我在寻找它时没有使用正确的术语。
Any clues how to do this?
任何线索如何做到这一点?
1 个解决方案
#1
1
First, I'd go for a NStextView instead, where the associated NSLayoutManager and NSTextStorage components are already set for you. Then, you can achieve what you are trying to do by doing the following steps:
首先,我会选择NStextView,其中已经为您设置了关联的NSLayoutManager和NSTextStorage组件。然后,您可以通过执行以下步骤来实现您要执行的操作:
-
Subclass NSATSTypesetter to draw custom glyphs for any characters you want by overriding :
子类NSATSTypesetter通过覆盖为您想要的任何字符绘制自定义字形:
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin
- (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex
- (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex
-
Subclass NSLayoutManager and set its type setter with the above one. Then override:
子类NSLayoutManager并使用上面的类设置它的类型。然后覆盖:
-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin { [self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin {[self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
}
-
Replace the layout manager of the NSTextView with the above one:
用上面的替换NSTextView的布局管理器:
[[textView textContainer] replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];
[[textView textContainer] replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];
Basically, you have to check NSLayoutManager and NSATSTypesetter classes for anything related to text custom drawing. Also there is a detailed guide about all this here.
基本上,您必须检查NSLayoutManager和NSATSTypesetter类以查找与文本自定义绘图相关的任何内容。此处还有详细的指南。
#1
1
First, I'd go for a NStextView instead, where the associated NSLayoutManager and NSTextStorage components are already set for you. Then, you can achieve what you are trying to do by doing the following steps:
首先,我会选择NStextView,其中已经为您设置了关联的NSLayoutManager和NSTextStorage组件。然后,您可以通过执行以下步骤来实现您要执行的操作:
-
Subclass NSATSTypesetter to draw custom glyphs for any characters you want by overriding :
子类NSATSTypesetter通过覆盖为您想要的任何字符绘制自定义字形:
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin
- (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex
- (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex
-
Subclass NSLayoutManager and set its type setter with the above one. Then override:
子类NSLayoutManager并使用上面的类设置它的类型。然后覆盖:
-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin { [self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin {[self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
}
-
Replace the layout manager of the NSTextView with the above one:
用上面的替换NSTextView的布局管理器:
[[textView textContainer] replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];
[[textView textContainer] replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];
Basically, you have to check NSLayoutManager and NSATSTypesetter classes for anything related to text custom drawing. Also there is a detailed guide about all this here.
基本上,您必须检查NSLayoutManager和NSATSTypesetter类以查找与文本自定义绘图相关的任何内容。此处还有详细的指南。