• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
underlining
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

underlining


  • Subject: underlining
  • From: Eric Slosser <email@hidden>
  • Date: Thu, 5 Feb 2009 18:00:28 -0500

I'm trying to draw underlined strings, but they come out wrong when the font is bold. So I made a sample app from the Cocoa template, and added a custom view to the main window. The code is below. But the output doesn't underline correctly. What am I doing wrong?







NSTextStorage* getTextStorage() {

	if( !s_textStorage ){

		s_textStorage = [[NSTextStorage alloc] init];
		NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
		NSTextContainer *textContainer = [[NSTextContainer alloc] init];
		[layoutManager addTextContainer:textContainer];
		[textContainer release];
		[s_textStorage addLayoutManager:layoutManager];
		[layoutManager release];
	}
	return s_textStorage;
}

- (void)drawRect:(NSRect)rect {

int fontSize = 41;
NSFontManager* fontManager = [NSFontManager sharedFontManager];
NSFont* boldFont = [fontManager fontWithFamily:@"Arial" traits:0 weight:5 size:fontSize];
// boldFont = [fontManager convertFont:font toHaveTrait:NSBoldFontMask];



NSMutableDictionary* attr = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[attr setObject:[NSNumber numberWithInt:NSSingleUnderlineStyle] forKey:NSUnderlineStyleAttributeName];


NSAttributedString* attrString = [[NSAttributedString alloc] initWithString:@"Sample Text" attributes:attr];

    NSTextStorage* textStorage = getTextStorage();
    [textStorage setAttributedString:attrString];

NSLayoutManager *layoutManager = (NSLayoutManager *)[[textStorage layoutManagers] objectAtIndex:0];
NSTextContainer *textContainer = (NSTextContainer *) [[layoutManager textContainers] objectAtIndex:0];


NSPoint point = NSMakePoint( 10, 10);
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
[[textContainer layoutManager] drawGlyphsForGlyphRange: glyphRange atPoint:point ];
}


_______________________________________________

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


  • Follow-Ups:
    • Re: underlining
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: How to transform a NSString to a NSString C string format?
  • Next by Date: Re: Transparency with PNG on NSImageView
  • Previous by thread: Re: How to transform a NSString to a NSString C string format?
  • Next by thread: Re: underlining
  • Index(es):
    • Date
    • Thread