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