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@8: #if TARGET_OS_IPHONE 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@8: #if TARGET_OS_IPHONE 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@10: if( (align & (kCALayerMinXMargin | kCALayerMaxXMargin)) == (kCALayerMinXMargin | kCALayerMaxXMargin) ) jens@1: mode = @"center"; jens@10: else { jens@10: if( align & kCALayerWidthSizable ) jens@10: mode = @"center"; jens@10: else if( align & kCALayerMinXMargin ) jens@10: mode = @"right"; jens@10: else jens@10: mode = @"left"; jens@10: align |= kCALayerWidthSizable; jens@10: } jens@1: label.alignmentMode = mode; jens@1: jens@8: // Get the bounds of the interior of the superlayer: jens@9: CGFloat yinset = 0; jens@1: if( [superlayer respondsToSelector: @selector(borderWidth)] ) jens@9: yinset += ((GGBLayer*)superlayer).borderWidth; jens@9: CGFloat xinset; jens@9: if( mode==@"center" ) jens@9: xinset = 0; jens@9: else jens@9: xinset = yinset + round(font.pointSize/3.0); jens@9: CGRect bounds = CGRectInset(superlayer.bounds, xinset,yinset); jens@8: jens@8: // Compute y position of bottom of layer's frame. (Remember, descender is negative!) jens@8: CGFloat y = bounds.origin.y; jens@8: CGFloat descender=font.descender, height=font.ascender-descender; jens@8: if( align & kCALayerHeightSizable ) { jens@8: // Vertical centering: jens@8: y += (bounds.size.height-height)/2.0; jens@8: #if ! TARGET_OS_IPHONE jens@8: y += descender/2.0; jens@4: #endif jens@8: align &= ~kCALayerHeightSizable; jens@8: } else if( align & kCALayerMinYMargin ) { jens@8: // Top alignment (Mac) or bottom (iPhone): jens@1: y += bounds.size.height - height; jens@8: } jens@8: jens@8: // Compute label's geometry: jens@4: label.bounds = CGRectMake(0, descender, jens@8: bounds.size.width, height); jens@1: label.anchorPoint = CGPointMake(0,0); jens@8: label.position = CGPointMake(bounds.origin.x,y); jens@1: jens@8: #if ! TARGET_OS_IPHONE jens@1: label.autoresizingMask = align; jens@4: #endif jens@1: [superlayer addSublayer: label]; jens@1: [label release]; jens@4: jens@8: #if 0 // for debugging layout, border the view jens@8: label.borderWidth = 1; jens@8: label.borderColor = kBlackColor; jens@8: #endif jens@4: jens@1: return label; jens@1: } jens@1: jens@1: jens@8: #if TARGET_OS_IPHONE 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@8: //float ascender=_font.ascender, descender=_font.descender, leading=_font.leading; jens@8: //bounds.size.height -= ascender-descender - leading; jens@8: 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: */