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

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


  • Subject: Re: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • From: Kevin Wojniak <email@hidden>
  • Date: Mon, 8 Mar 2010 12:36:10 -0800

You could try using -[NSMutableAttributedString setAlignment:range:]


On Mar 8, 2010, at 12:27 PM, Neil Clayton wrote:

>
> 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

_______________________________________________

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>
References: 
 >Unable to right align text when drawing via [NSAttributedString drawAt:] (From: Neil Clayton <email@hidden>)

  • Prev by Date: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • Next by Date: Re: Custom View in Toolbar
  • Previous by thread: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • Next by thread: Re: Unable to right align text when drawing via [NSAttributedString drawAt:]
  • Index(es):
    • Date
    • Thread