Re: Unable to right align text when drawing via [NSAttributedString drawAt:]
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 13:02:24 -0800
Never mind, ignore that. Use drawInRect instead of drawAtPoint. My guess is that when when you draw at a point, Cocoa obviously doesn't have a boundary to right (or center) align the text to, so it just defaults to left-aligned.
On Mar 8, 2010, at 12:36 PM, Kevin Wojniak wrote:
> 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
_______________________________________________
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