Source/GGBTextLayer.m
author Jens Alfke <jens@mooseyard.com>
Wed May 28 12:47:10 2008 -0700 (2008-05-28)
changeset 8 45c82a071aca
parent 4 d781b00f3ed4
child 9 a59acc683080
permissions -rw-r--r--
* Got it working with latest iPhone SDK.
* Fixed some text alignment issues that showed up on PlayingCards.
* Working on persistence and move-tracking for Game.
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@8
    21
#if TARGET_OS_IPHONE
jens@4
    22
    UIFont *font = [UIFont systemFontOfSize: fontSize];
jens@4
    23
#else
jens@4
    24
    NSFont *font = [NSFont systemFontOfSize: fontSize];
jens@4
    25
#endif
jens@4
    26
    return [self textLayerInSuperlayer: superlayer
jens@4
    27
                              withText: text
jens@4
    28
                                  font: font
jens@4
    29
                             alignment: align];
jens@4
    30
}
jens@4
    31
jens@4
    32
jens@4
    33
+ (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer
jens@4
    34
                               withText: (NSString*)text
jens@4
    35
                                   font: (id)inputFont
jens@4
    36
                              alignment: (enum CAAutoresizingMask) align
jens@4
    37
{
jens@1
    38
    GGBTextLayer *label = [[self alloc] init];
jens@1
    39
    label.string = text;
jens@1
    40
jens@8
    41
#if TARGET_OS_IPHONE
jens@4
    42
    UIFont *font = inputFont;
jens@4
    43
    [label setNeedsDisplay];
jens@4
    44
    label.needsDisplayOnBoundsChange = YES;
jens@1
    45
#else
jens@4
    46
    NSFont *font = inputFont;
jens@4
    47
    label.fontSize = font.pointSize;
jens@1
    48
#endif
jens@1
    49
    
jens@4
    50
    label.font = font;
jens@1
    51
    label.foregroundColor = kBlackColor;
jens@1
    52
    
jens@1
    53
    NSString *mode;
jens@1
    54
    if( align & kCALayerWidthSizable )
jens@1
    55
        mode = @"center";
jens@1
    56
    else if( align & kCALayerMinXMargin )
jens@1
    57
        mode = @"right";
jens@1
    58
    else
jens@1
    59
        mode = @"left";
jens@1
    60
    align |= kCALayerWidthSizable;
jens@1
    61
    label.alignmentMode = mode;
jens@1
    62
    
jens@8
    63
    // Get the bounds of the interior of the superlayer:
jens@8
    64
    CGFloat inset = round(font.pointSize/8);
jens@1
    65
    if( [superlayer respondsToSelector: @selector(borderWidth)] )
jens@1
    66
        inset += ((GGBLayer*)superlayer).borderWidth;
jens@1
    67
    CGRect bounds = CGRectInset(superlayer.bounds, inset, inset);
jens@8
    68
    if( mode==@"center" ) {
jens@8
    69
        // horizontal centering: ignore x inset:
jens@4
    70
        bounds = CGRectInset(bounds,-inset,0);
jens@8
    71
    }
jens@8
    72
    
jens@8
    73
    // Compute y position of bottom of layer's frame. (Remember, descender is negative!)
jens@8
    74
    CGFloat y = bounds.origin.y;
jens@8
    75
    CGFloat descender=font.descender, height=font.ascender-descender;
jens@8
    76
    if( align & kCALayerHeightSizable ) {
jens@8
    77
        // Vertical centering:
jens@8
    78
        y += (bounds.size.height-height)/2.0;
jens@8
    79
#if ! TARGET_OS_IPHONE
jens@8
    80
        y += descender/2.0;
jens@4
    81
#endif
jens@8
    82
        align &= ~kCALayerHeightSizable;
jens@8
    83
    } else if( align & kCALayerMinYMargin ) {
jens@8
    84
        // Top alignment (Mac) or bottom (iPhone):
jens@1
    85
        y += bounds.size.height - height;
jens@8
    86
    }
jens@8
    87
    
jens@8
    88
    // Compute label's geometry:
jens@4
    89
    label.bounds = CGRectMake(0, descender,
jens@8
    90
                              bounds.size.width, height);
jens@1
    91
    label.anchorPoint = CGPointMake(0,0);
jens@8
    92
    label.position = CGPointMake(bounds.origin.x,y);
jens@1
    93
    
jens@8
    94
#if ! TARGET_OS_IPHONE
jens@1
    95
    label.autoresizingMask = align;
jens@4
    96
#endif
jens@1
    97
    [superlayer addSublayer: label];
jens@1
    98
    [label release];
jens@4
    99
    
jens@8
   100
#if 0 // for debugging layout, border the view
jens@8
   101
    label.borderWidth = 1;
jens@8
   102
    label.borderColor = kBlackColor;
jens@8
   103
#endif
jens@4
   104
    
jens@1
   105
    return label;
jens@1
   106
}
jens@1
   107
jens@1
   108
jens@8
   109
#if TARGET_OS_IPHONE
jens@4
   110
@synthesize string=_string, font=_font, 
jens@1
   111
            foregroundColor=_foregroundColor, alignmentMode=_alignmentMode;
jens@4
   112
jens@4
   113
jens@4
   114
- (id) copyWithZone: (NSZone*)zone
jens@4
   115
{
jens@4
   116
    GGBTextLayer *clone = [super copyWithZone: zone];
jens@4
   117
    clone.string = _string;
jens@4
   118
    clone.font = _font;
jens@4
   119
    clone.foregroundColor = _foregroundColor;
jens@4
   120
    clone.alignmentMode = _alignmentMode;
jens@4
   121
    return clone;
jens@4
   122
}
jens@4
   123
jens@4
   124
jens@4
   125
- (void)drawInContext:(CGContextRef)ctx
jens@4
   126
{
jens@4
   127
    [super drawInContext: ctx];
jens@4
   128
    
jens@4
   129
    if( _string.length > 0 ) {
jens@4
   130
        CGContextSaveGState(ctx);
jens@4
   131
        UIGraphicsPushContext(ctx);
jens@4
   132
        
jens@4
   133
        if( _foregroundColor )
jens@4
   134
            CGContextSetFillColorWithColor(ctx, _foregroundColor);
jens@4
   135
        
jens@4
   136
        UITextAlignment align;
jens@4
   137
        if( [_alignmentMode isEqualToString: @"center"] )
jens@4
   138
            align = UITextAlignmentCenter;
jens@4
   139
        else if( [_alignmentMode isEqualToString: @"right"] )
jens@4
   140
            align = UITextAlignmentRight;
jens@4
   141
        else
jens@4
   142
            align = UITextAlignmentLeft;
jens@4
   143
        
jens@4
   144
        CGRect bounds = self.bounds;
jens@8
   145
        //float ascender=_font.ascender, descender=_font.descender, leading=_font.leading;
jens@8
   146
        //bounds.size.height -= ascender-descender - leading;
jens@8
   147
        
jens@4
   148
        [_string drawInRect: bounds 
jens@4
   149
                   withFont: _font
jens@4
   150
              lineBreakMode: UILineBreakModeClip
jens@4
   151
                  alignment: align];
jens@4
   152
        
jens@4
   153
        UIGraphicsPopContext();
jens@4
   154
        CGContextRestoreGState(ctx);
jens@4
   155
    }
jens@4
   156
}
jens@4
   157
jens@4
   158
jens@1
   159
#endif
jens@1
   160
jens@1
   161
jens@1
   162
@end
jens@4
   163
jens@4
   164
jens@4
   165
/*
jens@4
   166
 .times lt mm: (TimesLTMM)
jens@4
   167
 times new roman: (TimesNewRomanBoldItalic, TimesNewRomanItalic, TimesNewRoman, TimesNewRomanBold)
jens@4
   168
 phonepadtwo: (PhonepadTwo)
jens@4
   169
 hiragino kaku gothic pron w3: (HiraKakuProN-W3)
jens@4
   170
 helvetica neue: (HelveticaNeueBold, HelveticaNeue)
jens@4
   171
 trebuchet ms: (TrebuchetMSItalic, TrebuchetMSBoldItalic, TrebuchetMSBold, TrebuchetMS)
jens@4
   172
 courier new: (CourierNewBoldItalic, CourierNewBold, CourierNewItalic, CourierNew)
jens@4
   173
 arial unicode ms: (arialuni)
jens@4
   174
 georgia: (Georgia, GeorgiaBold, GeorgiaBoldItalic, GeorgiaItalic)
jens@4
   175
 zapfino: (Zapfino)
jens@4
   176
 arial rounded mt bold: (ArialRoundedMTBold)
jens@4
   177
 db lcd temp: (DB_LCD_Temp-Black)
jens@4
   178
 verdana: (Verdana, VerdanaItalic, VerdanaBoldItalic, VerdanaBold)
jens@4
   179
 american typewriter: (AmericanTypewriterCondensedBold, AmericanTypewriter)
jens@4
   180
 helvetica: (HelveticaBoldOblique, Helvetica, HelveticaOblique, HelveticaBold)
jens@4
   181
 lock clock: (LockClock)
jens@4
   182
 courier: (CourierBoldOblique, CourierOblique)
jens@4
   183
 hiragino kaku gothic pron w6: (HiraKakuProN-W6)
jens@4
   184
 arial: (ArialItalic, ArialBold, Arial, ArialBoldItalic)
jens@4
   185
 .helvetica lt mm: (HelveticaLTMM)
jens@4
   186
 stheiti: (STHeiti, STXihei)
jens@4
   187
 applegothic: (AppleGothicRegular)
jens@4
   188
 marker felt: (MarkerFeltThin)
jens@4
   189
*/