Getting accurate bounds for glyph range
Getting accurate bounds for glyph range
- Subject: Getting accurate bounds for glyph range
- From: Jeremy Dronfield <email@hidden>
- Date: Tue, 2 Mar 2004 17:56:15 +0000
I'm experimenting with Matt Gemmell's Rounded Floating Panel, using it
to display text instead of icons (sort of like the Large Type display
in Address Book). To this end, I've inserted a transparent text view in
place of the image view supplied. The point where I run into trouble is
getting the text view to resize accurately to the bounds of its text
storage. The following code represents my latest desperate attempt to
achieve this:
NSTextContainer *textContainer = [textView textContainer];
NSTextStorage *textStorage = [textView textStorage];
NSLayoutManager *layoutManager = [textContainer layoutManager];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:[NSArray
arrayWithObjects:[NSFont fontWithName:@"Lucida Grande Bold" size:40.0],
[NSColor whiteColor], nil] forKeys:[NSArray
arrayWithObjects:NSFontAttributeName, NSForegroundColorAttributeName,
nil]];
NSAttributedString *string = [[NSAttributedString alloc]
initWithString:@"Some Sample Text" attributes:attributes];
[textView setBackgroundColor:[NSColor colorWithCalibratedWhite:0.0
alpha:0.35]];
[textStorage appendAttributedString:string];
[string release];
[textStorage removeAttribute:NSParagraphStyleAttributeName
range:NSMakeRange(0,[textStorage length])];
NSRange effectiveRange = NSMakeRange(0, [textStorage length]);
NSRange glyphRange = [layoutManager
glyphRangeForCharacterRange:effectiveRange actualCharacterRange:NULL];
NSRect glyphRect = [layoutManager boundingRectForGlyphRange:glyphRange
inTextContainer:textContainer];
[textView setNeedsDisplayInRect:NSMakeRect(0,0,NSWidth(glyphRect),
NSHeight(glyphRect)) avoidAdditionalLayout:YES];
[[self window] setFrame:[textView frame] display:YES];
The result of this is that the text wraps (which I don't want), the
text view shrinks in width to the minimum allowed by the text, and the
text is cropped. The above example comes out as,
________
|Some |
|Sample|
-------
A longer sample came out as,
________
|A |
|Longer |
|Piece |
|of |
|Sample|
--------
As an alternative, I tried using [textStorage size] instead of trying
to get the glyph rect. The result of doing it that way came out
cropped, something like,
__________________
|Some Sample Tex|
----------------
This is driving me up the wall. I just can't track down where I'm going
wrong. Can anybody help?
-Jeremy
========================================
email@hidden
theLocustFarm.net:
- fractious fiction at
http://freespace.virgin.net/jeremy.dronfield
========================================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.