Source/GGBTextLayer.m
author Jens Alfke <jens@mooseyard.com>
Mon Mar 10 17:30:57 2008 -0700 (2008-03-10)
changeset 1 3eb7be1dd7b6
child 4 d781b00f3ed4
permissions -rw-r--r--
Tic-tac-toe works on the iPhone simulator!
     1 //
     2 //  GGBTextLayer.m
     3 //  GGB-iPhone
     4 //
     5 //  Created by Jens Alfke on 3/10/08.
     6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
     7 //
     8 
     9 #import "GGBTextLayer.h"
    10 #import "QuartzUtils.h"
    11 
    12 
    13 @implementation GGBTextLayer
    14 
    15 
    16 + (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer
    17                                withText: (NSString*)text
    18                                fontSize: (float) fontSize
    19                               alignment: (enum CAAutoresizingMask) align
    20 {
    21     GGBTextLayer *label = [[self alloc] init];
    22     label.string = text;
    23 
    24 #if TARGET_OS_ASPEN
    25     UIFont *font = [UIFont systemFontOfSize: fontSize];
    26 #else
    27     NSFont *font = [NSFont systemFontOfSize: fontSize];
    28     label.font = font;
    29 #endif
    30     
    31     label.fontSize = fontSize;
    32     label.foregroundColor = kBlackColor;
    33     
    34     NSString *mode;
    35     if( align & kCALayerWidthSizable )
    36         mode = @"center";
    37     else if( align & kCALayerMinXMargin )
    38         mode = @"right";
    39     else
    40         mode = @"left";
    41     align |= kCALayerWidthSizable;
    42     label.alignmentMode = mode;
    43     
    44     CGFloat inset = 3;
    45     if( [superlayer respondsToSelector: @selector(borderWidth)] )
    46         inset += ((GGBLayer*)superlayer).borderWidth;
    47     CGRect bounds = CGRectInset(superlayer.bounds, inset, inset);
    48     CGFloat height = font.ascender;
    49     CGFloat y = bounds.origin.y;
    50     if( align & kCALayerHeightSizable )
    51         y += (bounds.size.height-height)/2.0;
    52     else if( align & kCALayerMinYMargin )
    53         y += bounds.size.height - height;
    54     align &= ~kCALayerHeightSizable;
    55     label.bounds = CGRectMake(0, font.descender,
    56                               bounds.size.width, height - font.descender);
    57     label.position = CGPointMake(bounds.origin.x,y+font.descender);
    58     label.anchorPoint = CGPointMake(0,0);
    59     
    60     label.autoresizingMask = align;
    61     [superlayer addSublayer: label];
    62     [label release];
    63     return label;
    64 }
    65 
    66 
    67 #if TARGET_OS_ASPEN
    68 @synthesize string=_string, fontSize=_fontSize, 
    69             foregroundColor=_foregroundColor, alignmentMode=_alignmentMode;
    70 #endif
    71 
    72 
    73 @end