Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 16, 2012, 03:31 AM   #1
larswik
macrumors 65816
 
Join Date: Sep 2006
String drawInRect NSImage problem

I have a Mac program that I am combining images and strings into an NSImage and then saving it as a .tif file to the desktop. It all works just fine but as I started to add attributes for my string like color, shadow and stroke I ran into a problem.

In the included image you can see the stroke is also being offset / effected by the offset for the shadow. It works fine with a shadow or a stroke but when I use both it looks strange? It appears the stroke appears to inherits the attributes of the shadow for offset and blur? I grey'd out code that is not a problem to make it easier to read.

OSX 10.7.5
xcode 4.5.2

Code.
Code:
-(void)buildImage:(NSArray*)array{
    NSImage *toSave = [[NSImage alloc] initWithSize:CGSizeMake(1280, 720)];
    NSImage *imageSource = [[NSImage alloc]initWithData:[array objectAtIndex:0]];
    NSImage *rustPlate = [NSImage imageNamed:@"rusty-metal-plate.png"];

    NSString *itemName = [array objectAtIndex:1];
    NSString *itemWeight = [array objectAtIndex:3];
    NSString *itemCost = [array objectAtIndex:4];
    NSColor* tokenColor = [NSColor yellowColor];
    
    NSShadow* shadw = [[NSShadow alloc] init];
    [shadw setShadowColor:[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.4]];
    [shadw setShadowOffset:NSMakeSize( 5.0, -5.0 )];
    [shadw setShadowBlurRadius:1.0];
                                         
    NSDictionary *nameAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"Helvetica-Bold" size:82], NSFontAttributeName,tokenColor,NSForegroundColorAttributeName,[NSNumber numberWithFloat:-3.0],NSStrokeWidthAttributeName,shadw, NSShadowAttributeName, [NSColor blackColor],NSStrokeColorAttributeName, nil];
    
    NSDictionary *weightAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"Helvetica-Bold" size:36], NSFontAttributeName,tokenColor,NSForegroundColorAttributeName,[NSNumber numberWithFloat:-5.0],NSStrokeWidthAttributeName,
                                    [NSColor blackColor],NSStrokeColorAttributeName, nil];
    
    NSDictionary *costAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"MarkerFelt-Thin" size:140], NSFontAttributeName,tokenColor,NSForegroundColorAttributeName,[NSNumber numberWithFloat:-3.0],NSStrokeWidthAttributeName,
                                      [NSColor blackColor],NSStrokeColorAttributeName, nil];

    [toSave lockFocus];
    
    [rustPlate drawInRect:NSMakeRect(200,140,900,500) fromRect: NSZeroRect operation:NSCompositeCopy fraction:1.0];
    [imageSource drawInRect:NSMakeRect(650,200,400,400) fromRect: NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    [itemName drawInRect:NSMakeRect(260, 520, 450, 100) withAttributes: nameAttributes];
    [itemWeight drawInRect:NSMakeRect(260, 420, 450, 100) withAttributes: weightAttributes];
    [itemCost drawInRect:NSMakeRect(280, 180, 450, 250) withAttributes: costAttributes];
    
    [toSave unlockFocus];
    
    NSData *data = [toSave TIFFRepresentation];
    [data writeToFile:  @"/Users/larspro/Documents/ERad files/saved/file.tif" atomically: NO];
}
Any Ideas?
Attached Images
 
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Dec 16, 2012, 04:55 AM   #2
JoshDC
macrumors regular
 
Join Date: Apr 2009
Looks to me like it's a bug. You can see it's drawing a shadow for both the string's fill and stroke, but the stroke's shadow seems to be drawing on top of everything else. I'd send a bug report to Apple and see if they agree.

As a workaround, you can draw the shadow as a separate operation and draw the other string on top of it:

Code:
NSDictionary *nameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
								[NSFont fontWithName:@"Helvetica-Bold" size:82], NSFontAttributeName,
								tokenColor,NSForegroundColorAttributeName,
								[NSNumber numberWithFloat:-3.0],NSStrokeWidthAttributeName,
								[NSColor blackColor],NSStrokeColorAttributeName, nil];
NSDictionary *nameShadowAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
								[NSFont fontWithName:@"Helvetica-Bold" size:82], NSFontAttributeName,
								shadw, NSShadowAttributeName,
								[NSColor whiteColor],NSForegroundColorAttributeName, nil];

[toSave lockFocus];

// Draw the shadow only
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositePlusDarker];
[itemName drawInRect:NSMakeRect(260, 520, 450, 100) withAttributes: nameShadowAttributes];
[[NSGraphicsContext currentContext] restoreGraphicsState];

// Draw the stylized string
[itemName drawInRect:NSMakeRect(260, 520, 450, 100) withAttributes: nameAttributes];

[toSave unlockFocus];
JoshDC is offline   0 Reply With Quote
Old Dec 16, 2012, 03:32 PM   #3
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
That a good approach. I was dealing with this for most of my Saturday night. It's nice to know that it might just be a bug.

Thanks for the help.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Format string is not a string literal. larswik Mac Programming 5 Jan 17, 2014 10:45 PM
Search a string for a String of a specific format. ncaissie iPhone/iPad Programming 19 Nov 15, 2013 10:42 AM
How do I add effects to a NSImage or an ImageView Trisect Develop Mac Programming 2 Jul 8, 2013 02:54 AM
Problem in String to Date conversion Narendar Singh iPhone/iPad Programming 4 Oct 1, 2012 12:58 PM
How to transform this NSImage code for use in iOS hellomoto4 iPhone/iPad Programming 5 Jun 24, 2012 10:18 AM

Forum Jump

All times are GMT -5. The time now is 04:46 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC