Created
January 24, 2012 22:25
-
-
Save kylefox/1673108 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation RALabel | |
- (void) drawRect:(CGRect)rect | |
{ | |
// Drawing code | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman); | |
CGContextSetCharacterSpacing(context, 1); | |
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); | |
CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f ); | |
CGContextSetTextMatrix (context, myTextTransform); | |
// draw 1 but invisbly to get the string length. | |
CGPoint p =CGContextGetTextPosition(context); | |
float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2; | |
CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); | |
CGPoint v =CGContextGetTextPosition(context); | |
// calculate width and draw second one. | |
float width = v.x - p.x; | |
float centeredX =(self.frame.size.width- width)/2; | |
CGContextSetFillColorWithColor(context, [self.textColor CGColor]); | |
CGContextShowTextAtPoint(context, centeredX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment