• 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
Unable to right align text when drawing via [NSAttributedString drawAt:]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unable to right align text when drawing via [NSAttributedString drawAt:]


  • Subject: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • From: Neil Clayton <email@hidden>
  • Date: Tue, 9 Mar 2010 09:27:32 +1300
  • Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys

Hello All!

I seem unable to align text when drawing using drawAt:point.  The NSTextView shows it OK (so the attributes appear correct), but drawing of the text doesn't show alignment...

My code is:

- (void) awakeFromNib {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc addObserver:self selector:@selector(updateText:) name:NSTextViewDidChangeTypingAttributesNotification object:nil];
	[nc addObserver:self selector:@selector(updateText:) name:NSTextDidChangeNotification object:nil];

	NSString *str = @"This is a test\nAnd this text should be right aligned\nBut for some reason, in the image, isn't";
	NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle new] autorelease];
	[pStyle setAlignment:NSRightTextAlignment];
	NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:pStyle, NSParagraphStyleAttributeName, nil];
	NSAttributedString *as = [[[NSAttributedString alloc] initWithString:str attributes:attrs] autorelease];
	[[text textStorage] setAttributedString:as];
}

- (void) updateText:(NSNotification*)aNotification {
	[self textChanged:self];
}

- (IBAction) textChanged:(id)sender {
	NSAttributedString *string = [text attributedString];
	NSSize bounds = [string size];

	if(bounds.width > 0 && bounds.height > 0) {
		NSImage *image = [[[NSImage alloc] initWithSize:bounds] autorelease];
		[image lockFocus];
		@try {
			[string drawAtPoint:NSZeroPoint];
		} @finally {
			[image unlockFocus];
		}

		[view setImage:image];
	} else {
		[view setImage:nil];
	}
}

A sample of what I see when I run this is here:
http://dl.dropbox.com/u/421935/DrawingText/DrawingTest.png

I must be missing something really obvious.  Any ideas?

Neil Clayton
email@hidden




_______________________________________________

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: Unable to right align text when drawing via [NSAttributedString drawAt:]
      • From: Kevin Wojniak <email@hidden>
  • Prev by Date: is as high as mine
  • Next by Date: Re: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • Previous by thread: Re: Proper control for a list of actions
  • Next by thread: Re: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • Index(es):
    • Date
    • Thread