Skip to content

Commit

Permalink
Merge pull request TTTAttributedLabel#13 from LongWeekend/master
Browse files Browse the repository at this point in the history
Added vertical text centering, added shadow property per issue TTTAttributedLabel#10
  • Loading branch information
Mattt Thompson committed Aug 25, 2011
2 parents fa75132 + 6d1969e commit c624a11
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TTTAttributedLabelExample/TTTAttributedLabelExample.xcodeproj/project.xcworkspace
TTTAttributedLabelExample/TTTAttributedLabelExample.xcodeproj/xcuserdata
48 changes: 41 additions & 7 deletions TTTAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ - (NSArray *)detectedLinksInString:(NSString *)string range:(NSRange)range error
- (NSTextCheckingResult *)linkAtCharacterIndex:(CFIndex)idx;
- (NSTextCheckingResult *)linkAtPoint:(CGPoint)p;
- (NSUInteger)characterIndexAtPoint:(CGPoint)p;
- (void) drawFramesetter:(CTFramesetterRef)framesetter textRange:(CFRange)textRange inRect:(CGRect)rect context:(CGContextRef)c;
@end

@implementation TTTAttributedLabel
Expand Down Expand Up @@ -295,6 +296,15 @@ - (NSUInteger)characterIndexAtPoint:(CGPoint)p {
return idx;
}

- (void) drawFramesetter:(CTFramesetterRef)framesetter textRange:(CFRange)textRange inRect:(CGRect)rect context:(CGContextRef)c {
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, textRange, path, NULL);
CTFrameDraw(frame, c);
CFRelease(frame);
CFRelease(path);
}

#pragma mark -
#pragma mark TTTAttributedLabel

Expand Down Expand Up @@ -341,16 +351,40 @@ - (void)drawTextInRect:(CGRect)rect {

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(c, CGAffineTransformIdentity);

// Inverts the CTM to match iOS coordinates (otherwise text draws upside-down; Mac OS's system is different)
CGContextTranslateCTM(c, 0.0f, self.bounds.size.height);
CGContextScaleCTM(c, 1.0f, -1.0f);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef frame = CTFramesetterCreateFrame(self.framesetter, CFRangeMake(0, [self.attributedText length]), path, NULL);
CTFrameDraw(frame, c);

CFRelease(frame);
CFRelease(path);
CGRect textRect = rect;
CFRange textRange = CFRangeMake(0, [self.attributedText length]);
CFRange fitRange;

// First, adjust the text to be in the center vertically, if the text size is smaller than the drawing rect
CGSize textSize = CTFramesetterSuggestFrameSizeWithConstraints(self.framesetter, textRange, NULL, textRect.size, &fitRange);
if (textSize.height < textRect.size.height) {
CGFloat yOffset = (NSInteger)((textRect.size.height - textSize.height) / 2);
textRect.origin = CGPointMake(textRect.origin.x, textRect.origin.y + yOffset);
textRect.size = CGSizeMake(textRect.size.width, textRect.size.height - yOffset);
}

// Second, trace the shadow before the actual text, if we have one
if (self.shadowColor) {
CGRect shadowRect = textRect;
// We subtract the height, not add it, because the whole scene is inverted.
shadowRect.origin = CGPointMake(shadowRect.origin.x + self.shadowOffset.width, shadowRect.origin.y - self.shadowOffset.height);

// Override the text's color attribute to whatever the shadow color is
NSMutableAttributedString *shadowAttrString = [self.attributedText mutableCopy];
[shadowAttrString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)self.shadowColor.CGColor range:NSMakeRange(0, [self.attributedText length])];
CTFramesetterRef shadowFramesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)shadowAttrString);
[shadowAttrString release];

[self drawFramesetter:shadowFramesetter textRange:textRange inRect:shadowRect context:c];
}

// Finally, draw the text itself (on top of the shadow, if there is one)
[self drawFramesetter:self.framesetter textRange:textRange inRect:textRect context:c];
}

#pragma mark -
Expand Down

0 comments on commit c624a11

Please sign in to comment.