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