Re: Invisible characters in NSTextView
Re: Invisible characters in NSTextView
- Subject: Re: Invisible characters in NSTextView
- From: glenn andreas <email@hidden>
- Date: Mon, 3 Jan 2005 09:19:00 -0600
On Jan 3, 2005, at 3:40 AM, Peter Borg wrote:
Hi!
As far as I know setShowsControlCharacters/setShowsInvisibleCharacters
does not work or they only work with some special fonts. But it is
fairly easy to implement it yourself (but it does make it slightly
slower). What you need to do is use your own NSLayoutManager in your
NSTextView and override -
(void)drawGlyphsForGlyphRange:(NSRange)glyphRange
atPoint:(NSPoint)containerOrigin and there basically check if the
glyph is one that you want visible and, if so, draw it yourself.
There, in theory, exist fonts that setShowsInvisibleCharacters works,
but I've never found one (it's suppose to be a capability of the font),
so subclassing NSLayoutManager is the cleanest way to do this.
Something like this:
- (void)drawGlyphsForGlyphRange:(NSRange)glyphRange
atPoint:(NSPoint)containerOrigin
{
if (showInvisibleCharacters) {
completeString = [[self textStorage] string];
lengthToRedraw = NSMaxRange(glyphRange);
for (index = glyphRange.location; index < lengthToRedraw; index++) {
characterToCheck = [completeString characterAtIndex:index];
if (characterToCheck == '\t') {
pointToDrawAt = [self locationForGlyphAtIndex:index];
glyphFragment = [self lineFragmentRectForGlyphAtIndex:index
effectiveRange:NULL];
pointToDrawAt.x += glyphFragment.origin.x;
pointToDrawAt.y = glyphFragment.origin.y;
[tabCharacter drawAtPoint:pointToDrawAt withAttributes:attributes];
}
}
}
[super drawGlyphsForGlyphRange:glyphRange atPoint:containerOrigin];
}
You can check out some complete workable code at
http://smultron.sourceforge.net (the SMLLayoutManager class).
Unfortunately, the above code doesn't quite work correctly, since
characterAtIndex: is a character based index, and you are going through
glyph based indices. Now while these two are usually the same (and the
layout manager will insert null-glyphs to help maintain this), there is
no guarantee that it will be the same, and so you should convert the
glyph index to character index.
The original poster could use IDEKit
(http://projects.gandreas.com/idekit) and just drop in a view that
provides this capability (and a boatload of others).
Glenn Andreas email@hidden
<http://www.gandreas.com/> oh my!
Mad, Bad, and Dangerous to Know
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden