Source/GGBTextLayer.m
author snej@snej.local
Sun Jan 11 00:02:27 2009 -0800 (2009-01-11)
changeset 26 e7a464fb6d39
parent 9 a59acc683080
permissions -rw-r--r--
Added ball graphics to Xcode project, which fixes the Go game. Fixed Tic-Tac-Toe. Fixed an exception in the demo app when changing games.
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@10
    54
    if( (align & (kCALayerMinXMargin | kCALayerMaxXMargin)) == (kCALayerMinXMargin | kCALayerMaxXMargin) )
jens@1
    55
        mode = @"center";
jens@10
    56
    else {
jens@10
    57
        if( align & kCALayerWidthSizable )
jens@10
    58
            mode = @"center";
jens@10
    59
        else if( align & kCALayerMinXMargin )
jens@10
    60
            mode = @"right";
jens@10
    61
        else
jens@10
    62
            mode = @"left";
jens@10
    63
        align |= kCALayerWidthSizable;
jens@10
    64
    }
jens@1
    65
    label.alignmentMode = mode;
jens@1
    66
    
jens@8
    67
    // Get the bounds of the interior of the superlayer:
jens@9
    68
    CGFloat yinset = 0;
jens@1
    69
    if( [superlayer respondsToSelector: @selector(borderWidth)] )
jens@9
    70
        yinset += ((GGBLayer*)superlayer).borderWidth;
jens@9
    71
    CGFloat xinset;
jens@9
    72
    if( mode==@"center" )
jens@9
    73
        xinset = 0;
jens@9
    74
    else
jens@9
    75
        xinset = yinset + round(font.pointSize/3.0);
jens@9
    76
    CGRect bounds = CGRectInset(superlayer.bounds, xinset,yinset);
jens@8
    77
    
jens@8
    78
    // Compute y position of bottom of layer's frame. (Remember, descender is negative!)
jens@8
    79
    CGFloat y = bounds.origin.y;
jens@8
    80
    CGFloat descender=font.descender, height=font.ascender-descender;
jens@8
    81
    if( align & kCALayerHeightSizable ) {
jens@8
    82
        // Vertical centering:
jens@8
    83
        y += (bounds.size.height-height)/2.0;
jens@8
    84
#if ! TARGET_OS_IPHONE
jens@8
    85
        y += descender/2.0;
jens@4
    86
#endif
jens@8
    87
        align &= ~kCALayerHeightSizable;
jens@8
    88
    } else if( align & kCALayerMinYMargin ) {
jens@8
    89
        // Top alignment (Mac) or bottom (iPhone):
jens@1
    90
        y += bounds.size.height - height;
jens@8
    91
    }
jens@8
    92
    
jens@8
    93
    // Compute label's geometry:
jens@4
    94
    label.bounds = CGRectMake(0, descender,
jens@8
    95
                              bounds.size.width, height);
jens@1
    96
    label.anchorPoint = CGPointMake(0,0);
jens@8
    97
    label.position = CGPointMake(bounds.origin.x,y);
jens@1
    98
    
jens@8
    99
#if ! TARGET_OS_IPHONE
jens@1
   100
    label.autoresizingMask = align;
jens@4
   101
#endif
jens@1
   102
    [superlayer addSublayer: label];
jens@1
   103
    [label release];
jens@4
   104
    
jens@8
   105
#if 0 // for debugging layout, border the view
jens@8
   106
    label.borderWidth = 1;
jens@8
   107
    label.borderColor = kBlackColor;
jens@8
   108
#endif
jens@4
   109
    
jens@1
   110
    return label;
jens@1
   111
}
jens@1
   112
jens@1
   113
jens@8
   114
#if TARGET_OS_IPHONE
jens@4
   115
@synthesize string=_string, font=_font, 
jens@1
   116
            foregroundColor=_foregroundColor, alignmentMode=_alignmentMode;
jens@4
   117
jens@4
   118
jens@4
   119
- (id) copyWithZone: (NSZone*)zone
jens@4
   120
{
jens@4
   121
    GGBTextLayer *clone = [super copyWithZone: zone];
jens@4
   122
    clone.string = _string;
jens@4
   123
    clone.font = _font;
jens@4
   124
    clone.foregroundColor = _foregroundColor;
jens@4
   125
    clone.alignmentMode = _alignmentMode;
jens@4
   126
    return clone;
jens@4
   127
}
jens@4
   128
jens@4
   129
jens@4
   130
- (void)drawInContext:(CGContextRef)ctx
jens@4
   131
{
jens@4
   132
    [super drawInContext: ctx];
jens@4
   133
    
jens@4
   134
    if( _string.length > 0 ) {
jens@4
   135
        CGContextSaveGState(ctx);
jens@4
   136
        UIGraphicsPushContext(ctx);
jens@4
   137
        
jens@4
   138
        if( _foregroundColor )
jens@4
   139
            CGContextSetFillColorWithColor(ctx, _foregroundColor);
jens@4
   140
        
jens@4
   141
        UITextAlignment align;
jens@4
   142
        if( [_alignmentMode isEqualToString: @"center"] )
jens@4
   143
            align = UITextAlignmentCenter;
jens@4
   144
        else if( [_alignmentMode isEqualToString: @"right"] )
jens@4
   145
            align = UITextAlignmentRight;
jens@4
   146
        else
jens@4
   147
            align = UITextAlignmentLeft;
jens@4
   148
        
jens@4
   149
        CGRect bounds = self.bounds;
jens@8
   150
        //float ascender=_font.ascender, descender=_font.descender, leading=_font.leading;
jens@8
   151
        //bounds.size.height -= ascender-descender - leading;
jens@8
   152
        
jens@4
   153
        [_string drawInRect: bounds 
jens@4
   154
                   withFont: _font
jens@4
   155
              lineBreakMode: UILineBreakModeClip
jens@4
   156
                  alignment: align];
jens@4
   157
        
jens@4
   158
        UIGraphicsPopContext();
jens@4
   159
        CGContextRestoreGState(ctx);
jens@4
   160
    }
jens@4
   161
}
jens@4
   162
jens@4
   163
jens@1
   164
#endif
jens@1
   165
jens@1
   166
jens@1
   167
@end
jens@4
   168
jens@4
   169
jens@4
   170
/*
jens@4
   171
 .times lt mm: (TimesLTMM)
jens@4
   172
 times new roman: (TimesNewRomanBoldItalic, TimesNewRomanItalic, TimesNewRoman, TimesNewRomanBold)
jens@4
   173
 phonepadtwo: (PhonepadTwo)
jens@4
   174
 hiragino kaku gothic pron w3: (HiraKakuProN-W3)
jens@4
   175
 helvetica neue: (HelveticaNeueBold, HelveticaNeue)
jens@4
   176
 trebuchet ms: (TrebuchetMSItalic, TrebuchetMSBoldItalic, TrebuchetMSBold, TrebuchetMS)
jens@4
   177
 courier new: (CourierNewBoldItalic, CourierNewBold, CourierNewItalic, CourierNew)
jens@4
   178
 arial unicode ms: (arialuni)
jens@4
   179
 georgia: (Georgia, GeorgiaBold, GeorgiaBoldItalic, GeorgiaItalic)
jens@4
   180
 zapfino: (Zapfino)
jens@4
   181
 arial rounded mt bold: (ArialRoundedMTBold)
jens@4
   182
 db lcd temp: (DB_LCD_Temp-Black)
jens@4
   183
 verdana: (Verdana, VerdanaItalic, VerdanaBoldItalic, VerdanaBold)
jens@4
   184
 american typewriter: (AmericanTypewriterCondensedBold, AmericanTypewriter)
jens@4
   185
 helvetica: (HelveticaBoldOblique, Helvetica, HelveticaOblique, HelveticaBold)
jens@4
   186
 lock clock: (LockClock)
jens@4
   187
 courier: (CourierBoldOblique, CourierOblique)
jens@4
   188
 hiragino kaku gothic pron w6: (HiraKakuProN-W6)
jens@4
   189
 arial: (ArialItalic, ArialBold, Arial, ArialBoldItalic)
jens@4
   190
 .helvetica lt mm: (HelveticaLTMM)
jens@4
   191
 stheiti: (STHeiti, STXihei)
jens@4
   192
 applegothic: (AppleGothicRegular)
jens@4
   193
 marker felt: (MarkerFeltThin)
jens@4
   194
*/