Source/GGBTextLayer.m
changeset 2 7b0441db81e5
child 4 d781b00f3ed4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Source/GGBTextLayer.m	Mon Mar 10 17:32:04 2008 -0700
     1.3 @@ -0,0 +1,73 @@
     1.4 +//
     1.5 +//  GGBTextLayer.m
     1.6 +//  GGB-iPhone
     1.7 +//
     1.8 +//  Created by Jens Alfke on 3/10/08.
     1.9 +//  Copyright 2008 __MyCompanyName__. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "GGBTextLayer.h"
    1.13 +#import "QuartzUtils.h"
    1.14 +
    1.15 +
    1.16 +@implementation GGBTextLayer
    1.17 +
    1.18 +
    1.19 ++ (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer
    1.20 +                               withText: (NSString*)text
    1.21 +                               fontSize: (float) fontSize
    1.22 +                              alignment: (enum CAAutoresizingMask) align
    1.23 +{
    1.24 +    GGBTextLayer *label = [[self alloc] init];
    1.25 +    label.string = text;
    1.26 +
    1.27 +#if TARGET_OS_ASPEN
    1.28 +    UIFont *font = [UIFont systemFontOfSize: fontSize];
    1.29 +#else
    1.30 +    NSFont *font = [NSFont systemFontOfSize: fontSize];
    1.31 +    label.font = font;
    1.32 +#endif
    1.33 +    
    1.34 +    label.fontSize = fontSize;
    1.35 +    label.foregroundColor = kBlackColor;
    1.36 +    
    1.37 +    NSString *mode;
    1.38 +    if( align & kCALayerWidthSizable )
    1.39 +        mode = @"center";
    1.40 +    else if( align & kCALayerMinXMargin )
    1.41 +        mode = @"right";
    1.42 +    else
    1.43 +        mode = @"left";
    1.44 +    align |= kCALayerWidthSizable;
    1.45 +    label.alignmentMode = mode;
    1.46 +    
    1.47 +    CGFloat inset = 3;
    1.48 +    if( [superlayer respondsToSelector: @selector(borderWidth)] )
    1.49 +        inset += ((GGBLayer*)superlayer).borderWidth;
    1.50 +    CGRect bounds = CGRectInset(superlayer.bounds, inset, inset);
    1.51 +    CGFloat height = font.ascender;
    1.52 +    CGFloat y = bounds.origin.y;
    1.53 +    if( align & kCALayerHeightSizable )
    1.54 +        y += (bounds.size.height-height)/2.0;
    1.55 +    else if( align & kCALayerMinYMargin )
    1.56 +        y += bounds.size.height - height;
    1.57 +    align &= ~kCALayerHeightSizable;
    1.58 +    label.bounds = CGRectMake(0, font.descender,
    1.59 +                              bounds.size.width, height - font.descender);
    1.60 +    label.position = CGPointMake(bounds.origin.x,y+font.descender);
    1.61 +    label.anchorPoint = CGPointMake(0,0);
    1.62 +    
    1.63 +    label.autoresizingMask = align;
    1.64 +    [superlayer addSublayer: label];
    1.65 +    [label release];
    1.66 +    return label;
    1.67 +}
    1.68 +
    1.69 +
    1.70 +#if TARGET_OS_ASPEN
    1.71 +@synthesize string=_string, fontSize=_fontSize, 
    1.72 +            foregroundColor=_foregroundColor, alignmentMode=_alignmentMode;
    1.73 +#endif
    1.74 +
    1.75 +
    1.76 +@end