jens@1: // jens@1: // GGBTextLayer.m jens@1: // GGB-iPhone jens@1: // jens@1: // Created by Jens Alfke on 3/10/08. jens@1: // Copyright 2008 __MyCompanyName__. All rights reserved. jens@1: // jens@1: jens@1: #import "GGBTextLayer.h" jens@1: #import "QuartzUtils.h" jens@1: jens@1: jens@1: @implementation GGBTextLayer jens@1: jens@1: jens@1: + (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer jens@1: withText: (NSString*)text jens@1: fontSize: (float) fontSize jens@1: alignment: (enum CAAutoresizingMask) align jens@1: { jens@1: GGBTextLayer *label = [[self alloc] init]; jens@1: label.string = text; jens@1: jens@1: #if TARGET_OS_ASPEN jens@1: UIFont *font = [UIFont systemFontOfSize: fontSize]; jens@1: #else jens@1: NSFont *font = [NSFont systemFontOfSize: fontSize]; jens@1: label.font = font; jens@1: #endif jens@1: jens@1: label.fontSize = fontSize; jens@1: label.foregroundColor = kBlackColor; jens@1: jens@1: NSString *mode; jens@1: if( align & kCALayerWidthSizable ) jens@1: mode = @"center"; jens@1: else if( align & kCALayerMinXMargin ) jens@1: mode = @"right"; jens@1: else jens@1: mode = @"left"; jens@1: align |= kCALayerWidthSizable; jens@1: label.alignmentMode = mode; jens@1: jens@1: CGFloat inset = 3; jens@1: if( [superlayer respondsToSelector: @selector(borderWidth)] ) jens@1: inset += ((GGBLayer*)superlayer).borderWidth; jens@1: CGRect bounds = CGRectInset(superlayer.bounds, inset, inset); jens@1: CGFloat height = font.ascender; jens@1: CGFloat y = bounds.origin.y; jens@1: if( align & kCALayerHeightSizable ) jens@1: y += (bounds.size.height-height)/2.0; jens@1: else if( align & kCALayerMinYMargin ) jens@1: y += bounds.size.height - height; jens@1: align &= ~kCALayerHeightSizable; jens@1: label.bounds = CGRectMake(0, font.descender, jens@1: bounds.size.width, height - font.descender); jens@1: label.position = CGPointMake(bounds.origin.x,y+font.descender); jens@1: label.anchorPoint = CGPointMake(0,0); jens@1: jens@1: label.autoresizingMask = align; jens@1: [superlayer addSublayer: label]; jens@1: [label release]; jens@1: return label; jens@1: } jens@1: jens@1: jens@1: #if TARGET_OS_ASPEN jens@1: @synthesize string=_string, fontSize=_fontSize, jens@1: foregroundColor=_foregroundColor, alignmentMode=_alignmentMode; jens@1: #endif jens@1: jens@1: jens@1: @end