Source/GGBTextLayer.m
author Jens Alfke <jens@mooseyard.com>
Sun Mar 16 15:06:47 2008 -0700 (2008-03-16)
changeset 7 428a194e3e59
parent 1 3eb7be1dd7b6
child 8 45c82a071aca
permissions -rw-r--r--
Game class now tracks board state and moves, as strings, and can step through its history.
Fixed another bug in Go (you could drag your captured stones back to the board!)
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@4
    21
#if TARGET_OS_ASPEN
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@1
    41
#if TARGET_OS_ASPEN
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@1
    63
    CGFloat inset = 3;
jens@1
    64
    if( [superlayer respondsToSelector: @selector(borderWidth)] )
jens@1
    65
        inset += ((GGBLayer*)superlayer).borderWidth;
jens@1
    66
    CGRect bounds = CGRectInset(superlayer.bounds, inset, inset);
jens@4
    67
    if( mode==@"center" )
jens@4
    68
        bounds = CGRectInset(bounds,-inset,0);
jens@1
    69
    CGFloat height = font.ascender;
jens@4
    70
    float descender = font.descender;
jens@4
    71
#if TARGET_OS_ASPEN
jens@4
    72
    descender = -descender;
jens@4
    73
#endif
jens@1
    74
    CGFloat y = bounds.origin.y;
jens@4
    75
    if( align & kCALayerHeightSizable ) {
jens@1
    76
        y += (bounds.size.height-height)/2.0;
jens@4
    77
#if TARGET_OS_ASPEN
jens@4
    78
        y -= descender/2.0;
jens@4
    79
#endif
jens@4
    80
    } else if( align & kCALayerMinYMargin )
jens@1
    81
        y += bounds.size.height - height;
jens@1
    82
    align &= ~kCALayerHeightSizable;
jens@4
    83
    label.bounds = CGRectMake(0, descender,
jens@4
    84
                              bounds.size.width, height - descender);
jens@4
    85
    label.position = CGPointMake(bounds.origin.x,y+descender);
jens@1
    86
    label.anchorPoint = CGPointMake(0,0);
jens@1
    87
    
jens@4
    88
#if ! TARGET_OS_ASPEN
jens@1
    89
    label.autoresizingMask = align;
jens@4
    90
#endif
jens@1
    91
    [superlayer addSublayer: label];
jens@1
    92
    [label release];
jens@4
    93
    
jens@4
    94
    //label.borderWidth = 1;
jens@4
    95
    //label.borderColor = kBlackColor;
jens@4
    96
    
jens@1
    97
    return label;
jens@1
    98
}
jens@1
    99
jens@1
   100
jens@1
   101
#if TARGET_OS_ASPEN
jens@4
   102
@synthesize string=_string, font=_font, 
jens@1
   103
            foregroundColor=_foregroundColor, alignmentMode=_alignmentMode;
jens@4
   104
jens@4
   105
jens@4
   106
- (id) copyWithZone: (NSZone*)zone
jens@4
   107
{
jens@4
   108
    GGBTextLayer *clone = [super copyWithZone: zone];
jens@4
   109
    clone.string = _string;
jens@4
   110
    clone.font = _font;
jens@4
   111
    clone.foregroundColor = _foregroundColor;
jens@4
   112
    clone.alignmentMode = _alignmentMode;
jens@4
   113
    return clone;
jens@4
   114
}
jens@4
   115
jens@4
   116
jens@4
   117
- (void)drawInContext:(CGContextRef)ctx
jens@4
   118
{
jens@4
   119
    [super drawInContext: ctx];
jens@4
   120
    
jens@4
   121
    if( _string.length > 0 ) {
jens@4
   122
        CGContextSaveGState(ctx);
jens@4
   123
        UIGraphicsPushContext(ctx);
jens@4
   124
        
jens@4
   125
        if( _foregroundColor )
jens@4
   126
            CGContextSetFillColorWithColor(ctx, _foregroundColor);
jens@4
   127
        
jens@4
   128
        UITextAlignment align;
jens@4
   129
        if( [_alignmentMode isEqualToString: @"center"] )
jens@4
   130
            align = UITextAlignmentCenter;
jens@4
   131
        else if( [_alignmentMode isEqualToString: @"right"] )
jens@4
   132
            align = UITextAlignmentRight;
jens@4
   133
        else
jens@4
   134
            align = UITextAlignmentLeft;
jens@4
   135
        
jens@4
   136
        CGRect bounds = self.bounds;
jens@4
   137
        bounds.origin.y += _font.ascender+_font.descender - _font.leading;
jens@4
   138
        [_string drawInRect: bounds 
jens@4
   139
                   withFont: _font
jens@4
   140
              lineBreakMode: UILineBreakModeClip
jens@4
   141
                  alignment: align];
jens@4
   142
        
jens@4
   143
        UIGraphicsPopContext();
jens@4
   144
        CGContextRestoreGState(ctx);
jens@4
   145
    }
jens@4
   146
}
jens@4
   147
jens@4
   148
jens@1
   149
#endif
jens@1
   150
jens@1
   151
jens@1
   152
@end
jens@4
   153
jens@4
   154
jens@4
   155
/*
jens@4
   156
 .times lt mm: (TimesLTMM)
jens@4
   157
 times new roman: (TimesNewRomanBoldItalic, TimesNewRomanItalic, TimesNewRoman, TimesNewRomanBold)
jens@4
   158
 phonepadtwo: (PhonepadTwo)
jens@4
   159
 hiragino kaku gothic pron w3: (HiraKakuProN-W3)
jens@4
   160
 helvetica neue: (HelveticaNeueBold, HelveticaNeue)
jens@4
   161
 trebuchet ms: (TrebuchetMSItalic, TrebuchetMSBoldItalic, TrebuchetMSBold, TrebuchetMS)
jens@4
   162
 courier new: (CourierNewBoldItalic, CourierNewBold, CourierNewItalic, CourierNew)
jens@4
   163
 arial unicode ms: (arialuni)
jens@4
   164
 georgia: (Georgia, GeorgiaBold, GeorgiaBoldItalic, GeorgiaItalic)
jens@4
   165
 zapfino: (Zapfino)
jens@4
   166
 arial rounded mt bold: (ArialRoundedMTBold)
jens@4
   167
 db lcd temp: (DB_LCD_Temp-Black)
jens@4
   168
 verdana: (Verdana, VerdanaItalic, VerdanaBoldItalic, VerdanaBold)
jens@4
   169
 american typewriter: (AmericanTypewriterCondensedBold, AmericanTypewriter)
jens@4
   170
 helvetica: (HelveticaBoldOblique, Helvetica, HelveticaOblique, HelveticaBold)
jens@4
   171
 lock clock: (LockClock)
jens@4
   172
 courier: (CourierBoldOblique, CourierOblique)
jens@4
   173
 hiragino kaku gothic pron w6: (HiraKakuProN-W6)
jens@4
   174
 arial: (ArialItalic, ArialBold, Arial, ArialBoldItalic)
jens@4
   175
 .helvetica lt mm: (HelveticaLTMM)
jens@4
   176
 stheiti: (STHeiti, STXihei)
jens@4
   177
 applegothic: (AppleGothicRegular)
jens@4
   178
 marker felt: (MarkerFeltThin)
jens@4
   179
*/