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