* Working on 3D rotation of game board.
* Added some colored balls ("drawn" myself using Photoshop glass effect.)
5 // Created by Jens Alfke on 3/10/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "GGBTextLayer.h"
10 #import "QuartzUtils.h"
13 @implementation GGBTextLayer
16 + (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer
17 withText: (NSString*)text
18 fontSize: (float) fontSize
19 alignment: (enum CAAutoresizingMask) align
22 UIFont *font = [UIFont systemFontOfSize: fontSize];
24 NSFont *font = [NSFont systemFontOfSize: fontSize];
26 return [self textLayerInSuperlayer: superlayer
33 + (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer
34 withText: (NSString*)text
36 alignment: (enum CAAutoresizingMask) align
38 GGBTextLayer *label = [[self alloc] init];
42 UIFont *font = inputFont;
43 [label setNeedsDisplay];
44 label.needsDisplayOnBoundsChange = YES;
46 NSFont *font = inputFont;
47 label.fontSize = font.pointSize;
51 label.foregroundColor = kBlackColor;
54 if( (align & (kCALayerMinXMargin | kCALayerMaxXMargin)) == (kCALayerMinXMargin | kCALayerMaxXMargin) )
57 if( align & kCALayerWidthSizable )
59 else if( align & kCALayerMinXMargin )
63 align |= kCALayerWidthSizable;
65 label.alignmentMode = mode;
67 // Get the bounds of the interior of the superlayer:
69 if( [superlayer respondsToSelector: @selector(borderWidth)] )
70 yinset += ((GGBLayer*)superlayer).borderWidth;
75 xinset = yinset + round(font.pointSize/3.0);
76 CGRect bounds = CGRectInset(superlayer.bounds, xinset,yinset);
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
87 align &= ~kCALayerHeightSizable;
88 } else if( align & kCALayerMinYMargin ) {
89 // Top alignment (Mac) or bottom (iPhone):
90 y += bounds.size.height - height;
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);
99 #if ! TARGET_OS_IPHONE
100 label.autoresizingMask = align;
102 [superlayer addSublayer: label];
105 #if 0 // for debugging layout, border the view
106 label.borderWidth = 1;
107 label.borderColor = kBlackColor;
115 @synthesize string=_string, font=_font,
116 foregroundColor=_foregroundColor, alignmentMode=_alignmentMode;
119 - (id) copyWithZone: (NSZone*)zone
121 GGBTextLayer *clone = [super copyWithZone: zone];
122 clone.string = _string;
124 clone.foregroundColor = _foregroundColor;
125 clone.alignmentMode = _alignmentMode;
130 - (void)drawInContext:(CGContextRef)ctx
132 [super drawInContext: ctx];
134 if( _string.length > 0 ) {
135 CGContextSaveGState(ctx);
136 UIGraphicsPushContext(ctx);
138 if( _foregroundColor )
139 CGContextSetFillColorWithColor(ctx, _foregroundColor);
141 UITextAlignment align;
142 if( [_alignmentMode isEqualToString: @"center"] )
143 align = UITextAlignmentCenter;
144 else if( [_alignmentMode isEqualToString: @"right"] )
145 align = UITextAlignmentRight;
147 align = UITextAlignmentLeft;
149 CGRect bounds = self.bounds;
150 //float ascender=_font.ascender, descender=_font.descender, leading=_font.leading;
151 //bounds.size.height -= ascender-descender - leading;
153 [_string drawInRect: bounds
155 lineBreakMode: UILineBreakModeClip
158 UIGraphicsPopContext();
159 CGContextRestoreGState(ctx);
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)
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)