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@4: #if TARGET_OS_ASPEN jens@4: UIFont *font = [UIFont systemFontOfSize: fontSize]; jens@4: #else jens@4: NSFont *font = [NSFont systemFontOfSize: fontSize]; jens@4: #endif jens@4: return [self textLayerInSuperlayer: superlayer jens@4: withText: text jens@4: font: font jens@4: alignment: align]; jens@4: } jens@4: jens@4: jens@4: + (GGBTextLayer*) textLayerInSuperlayer: (CALayer*)superlayer jens@4: withText: (NSString*)text jens@4: font: (id)inputFont jens@4: alignment: (enum CAAutoresizingMask) align jens@4: { jens@1: GGBTextLayer *label = [[self alloc] init]; jens@1: label.string = text; jens@1: jens@1: #if TARGET_OS_ASPEN jens@4: UIFont *font = inputFont; jens@4: [label setNeedsDisplay]; jens@4: label.needsDisplayOnBoundsChange = YES; jens@1: #else jens@4: NSFont *font = inputFont; jens@4: label.fontSize = font.pointSize; jens@1: #endif jens@1: jens@4: label.font = font; 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@4: if( mode==@"center" ) jens@4: bounds = CGRectInset(bounds,-inset,0); jens@1: CGFloat height = font.ascender; jens@4: float descender = font.descender; jens@4: #if TARGET_OS_ASPEN jens@4: descender = -descender; jens@4: #endif jens@1: CGFloat y = bounds.origin.y; jens@4: if( align & kCALayerHeightSizable ) { jens@1: y += (bounds.size.height-height)/2.0; jens@4: #if TARGET_OS_ASPEN jens@4: y -= descender/2.0; jens@4: #endif jens@4: } else if( align & kCALayerMinYMargin ) jens@1: y += bounds.size.height - height; jens@1: align &= ~kCALayerHeightSizable; jens@4: label.bounds = CGRectMake(0, descender, jens@4: bounds.size.width, height - descender); jens@4: label.position = CGPointMake(bounds.origin.x,y+descender); jens@1: label.anchorPoint = CGPointMake(0,0); jens@1: jens@4: #if ! TARGET_OS_ASPEN jens@1: label.autoresizingMask = align; jens@4: #endif jens@1: [superlayer addSublayer: label]; jens@1: [label release]; jens@4: jens@4: //label.borderWidth = 1; jens@4: //label.borderColor = kBlackColor; jens@4: jens@1: return label; jens@1: } jens@1: jens@1: jens@1: #if TARGET_OS_ASPEN jens@4: @synthesize string=_string, font=_font, jens@1: foregroundColor=_foregroundColor, alignmentMode=_alignmentMode; jens@4: jens@4: jens@4: - (id) copyWithZone: (NSZone*)zone jens@4: { jens@4: GGBTextLayer *clone = [super copyWithZone: zone]; jens@4: clone.string = _string; jens@4: clone.font = _font; jens@4: clone.foregroundColor = _foregroundColor; jens@4: clone.alignmentMode = _alignmentMode; jens@4: return clone; jens@4: } jens@4: jens@4: jens@4: - (void)drawInContext:(CGContextRef)ctx jens@4: { jens@4: [super drawInContext: ctx]; jens@4: jens@4: if( _string.length > 0 ) { jens@4: CGContextSaveGState(ctx); jens@4: UIGraphicsPushContext(ctx); jens@4: jens@4: if( _foregroundColor ) jens@4: CGContextSetFillColorWithColor(ctx, _foregroundColor); jens@4: jens@4: UITextAlignment align; jens@4: if( [_alignmentMode isEqualToString: @"center"] ) jens@4: align = UITextAlignmentCenter; jens@4: else if( [_alignmentMode isEqualToString: @"right"] ) jens@4: align = UITextAlignmentRight; jens@4: else jens@4: align = UITextAlignmentLeft; jens@4: jens@4: CGRect bounds = self.bounds; jens@4: bounds.origin.y += _font.ascender+_font.descender - _font.leading; jens@4: [_string drawInRect: bounds jens@4: withFont: _font jens@4: lineBreakMode: UILineBreakModeClip jens@4: alignment: align]; jens@4: jens@4: UIGraphicsPopContext(); jens@4: CGContextRestoreGState(ctx); jens@4: } jens@4: } jens@4: jens@4: jens@1: #endif jens@1: jens@1: jens@1: @end jens@4: jens@4: jens@4: /* jens@4: .times lt mm: (TimesLTMM) jens@4: times new roman: (TimesNewRomanBoldItalic, TimesNewRomanItalic, TimesNewRoman, TimesNewRomanBold) jens@4: phonepadtwo: (PhonepadTwo) jens@4: hiragino kaku gothic pron w3: (HiraKakuProN-W3) jens@4: helvetica neue: (HelveticaNeueBold, HelveticaNeue) jens@4: trebuchet ms: (TrebuchetMSItalic, TrebuchetMSBoldItalic, TrebuchetMSBold, TrebuchetMS) jens@4: courier new: (CourierNewBoldItalic, CourierNewBold, CourierNewItalic, CourierNew) jens@4: arial unicode ms: (arialuni) jens@4: georgia: (Georgia, GeorgiaBold, GeorgiaBoldItalic, GeorgiaItalic) jens@4: zapfino: (Zapfino) jens@4: arial rounded mt bold: (ArialRoundedMTBold) jens@4: db lcd temp: (DB_LCD_Temp-Black) jens@4: verdana: (Verdana, VerdanaItalic, VerdanaBoldItalic, VerdanaBold) jens@4: american typewriter: (AmericanTypewriterCondensedBold, AmericanTypewriter) jens@4: helvetica: (HelveticaBoldOblique, Helvetica, HelveticaOblique, HelveticaBold) jens@4: lock clock: (LockClock) jens@4: courier: (CourierBoldOblique, CourierOblique) jens@4: hiragino kaku gothic pron w6: (HiraKakuProN-W6) jens@4: arial: (ArialItalic, ArialBold, Arial, ArialBoldItalic) jens@4: .helvetica lt mm: (HelveticaLTMM) jens@4: stheiti: (STHeiti, STXihei) jens@4: applegothic: (AppleGothicRegular) jens@4: marker felt: (MarkerFeltThin) jens@4: */