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