Re: A Tab after 4 SPACEs
Re: A Tab after 4 SPACEs
- Subject: Re: A Tab after 4 SPACEs
- From: Alastair Houghton <email@hidden>
- Date: Thu, 27 Mar 2008 23:53:14 +0000
On 27 Mar 2008, at 16:12, Gerriet M. Denkmann wrote:
Current I am using:
NSString *dummyString = [ NSString stringWithUTF8String: " " ];
NSTextView *dummyTextView = [ [ NSTextView alloc ] initWithFrame:
NSMakeRect(0,0,1e4,1e4) ];
[ dummyTextView setString: dummyString ];
[ dummyTextView setFont: usedFont ];
NSLayoutManager *dummyLayoutManager = [ dummyTextView layoutManager ];
[ dummyLayoutManager setUsesScreenFonts: useScreenFont ];
unsigned int length = [ dummyString length ];
NSRange glyphRange = [ dummyLayoutManager
glyphRangeForCharacterRange: NSMakeRange(0,length)
actualCharacterRange: NULL ];
unsigned int glyphIndex = glyphRange.location;
NSTextContainer *aTextContainer = [ dummyLayoutManager
textContainerForGlyphAtIndex: glyphIndex effectiveRange: NULL ];
NSRect boundingRect = [ dummyLayoutManager
boundingRectForGlyphRange: glyphRange inTextContainer:
aTextContainer];
[dummyTextView release];
float widtOfDummyString = boundingRect.size.width;
This looks incredible clumsy. Do you know of any better way?
How about the following (largely untested; I did check that the glyph
returned seemed sane):
NSGlyph glyph = [usedFont glyphWithName:@"space"];
NSSize size;
[usedFont getAdvancements:&size forGlyphs:&glyph count:1];
("space" is the standard PostScript name for the space glyph; see,
e.g. <http://www.adobe.com/devnet/opentype/archives/glyph.html>)
Alternatively you could use the Core Text function
CTFontGetGlyphsForCharacters() (but you'd need a CTFontRef rather than
an NSFont), or you could implement a lightweight NSGlyphStorage and
use either the shared NSGlyphGenerator or the one from your layout
manager.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden