From 297ba78174d5b6c5f63ed62eb249a3d98b622b8f Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Thu, 16 Aug 2012 14:48:22 -0400 Subject: [PATCH 01/18] ARC and truncated text support --- MTLabel/MTLabel.h | 19 +----- MTLabel/MTLabel.m | 156 +++++++++++++++------------------------------- 2 files changed, 53 insertions(+), 122 deletions(-) diff --git a/MTLabel/MTLabel.h b/MTLabel/MTLabel.h index b16dcb1..cc80a89 100644 --- a/MTLabel/MTLabel.h +++ b/MTLabel/MTLabel.h @@ -31,23 +31,10 @@ typedef enum { } MTLabelTextAlignment; -@interface MTLabel : UIView { - - int _numberOfLines; - CGFloat _lineHeight; - CGFloat _minimumFontSize; - NSString *_text; - UIColor *_fontColor, *_highlightColor; - UIFont *_font; - BOOL _limitToNumberOfLines; - BOOL _shouldResizeToFit; - MTLabelTextAlignment _textAlignment; - id delegate; - BOOL adjustSizeToFit; - -} +@interface MTLabel : UIView @property (nonatomic, readwrite, setter = setNumberOfLines:, getter = numberOfLines) int _numberOfLines; +@property (nonatomic, readwrite, setter = setMaxNumberOfLines:, getter = numberOfLines) int _maxNumberOfLines; @property (nonatomic, readwrite, setter = setLineHeight:, getter = lineHeight) CGFloat _lineHeight; @property (nonatomic, readonly) CGFloat _textHeight; @property (nonatomic, readwrite, setter = setMinimumFontSize:, getter = minimumFontSize) CGFloat _minimumFontSize; @@ -58,7 +45,7 @@ typedef enum { @property (nonatomic, readwrite, setter = setLimitToNumberOfLines:, getter = limitToNumberOfLines) BOOL _limitToNumberOfLines; @property (nonatomic, readwrite, setter = setResizeToFitText:, getter = resizeToFitText) BOOL _shouldResizeToFit; @property (nonatomic, readwrite, setter = setTextAlignment:, getter = textAlignment) MTLabelTextAlignment _textAlignment; -@property (nonatomic, assign) id delegate; +@property (nonatomic, weak) id delegate; @property (nonatomic, readwrite, setter = setAdjustSizeToFit:, getter = adjustSizeToFit) BOOL _adjustSizeToFit; @property (nonatomic, readwrite) CGFloat shadowOffset; diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index bbde323..884d436 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -24,7 +24,7 @@ @interface MTLabel () --(void)drawTransparentBackground; +- (void)drawTransparentBackground; @end @@ -44,7 +44,7 @@ @implementation MTLabel @synthesize _text; @synthesize _lineHeight, _textHeight, _minimumFontSize; -@synthesize _numberOfLines; +@synthesize _numberOfLines, _maxNumberOfLines; @synthesize _font; @synthesize _fontColor, _fontHighlightColor; @synthesize _limitToNumberOfLines, _shouldResizeToFit; @@ -56,107 +56,61 @@ @implementation MTLabel #pragma mark - Setters --(void)setNumberOfLines:(int)numberOfLines { - - +- (void)setNumberOfLines:(int)numberOfLines { if (numberOfLines != _numberOfLines) { - _numberOfLines = numberOfLines; [self setNeedsDisplay]; - } } --(void)setLineHeight:(CGFloat)lineHeight { - +- (void)setLineHeight:(CGFloat)lineHeight { if (lineHeight != _lineHeight) { - _lineHeight = lineHeight; [self setNeedsDisplay]; - } } --(void)setText:(NSString *)text { - +- (void)setText:(NSString *)text { if (text != _text) { - - if (_text) { - - [_text release]; - _text = nil; - } - - _text = [text retain]; + _text = text; [self setNeedsDisplay]; - } } --(void)setFont:(UIFont *)font { - - if (font != _font) { - - if (_font) { - - [_font release]; - _font = nil; - - } - - _font = [font retain]; +- (void)setFont:(UIFont *)font { +if (font != _font) { + _font = font; self._lineHeight = _font.lineHeight; [self setNeedsDisplay]; - } } --(void)setFontColor:(UIColor *)fontColor { - +- (void)setFontColor:(UIColor *)fontColor { if (fontColor != _fontColor) { - - if (_fontColor) { - - [_fontColor release]; - _fontColor = nil; - - } - - _fontColor = [fontColor retain]; + _fontColor = fontColor; [self setNeedsDisplay]; } } - (void)setFontHighlightColor:(UIColor *)fontHighlightColor { if (fontHighlightColor != _fontHighlightColor) { - if (_fontHighlightColor) { - [_fontHighlightColor release]; - _fontHighlightColor = nil; - } - _fontHighlightColor = [fontHighlightColor retain]; + _fontHighlightColor = fontHighlightColor; [self setNeedsDisplay]; } } --(void)setLimitToNumberOfLines:(BOOL)limitToNumberOfLines { - +- (void)setLimitToNumberOfLines:(BOOL)limitToNumberOfLines { if (_limitToNumberOfLines != limitToNumberOfLines) { - _limitToNumberOfLines = limitToNumberOfLines; [self setNeedsDisplay]; - } - } --(void)setResizeToFitText:(BOOL)resizeToFitText { - - +- (void)setResizeToFitText:(BOOL)resizeToFitText { if (_shouldResizeToFit != resizeToFitText) { - _shouldResizeToFit = resizeToFitText; [self setNeedsDisplay]; } @@ -164,15 +118,11 @@ -(void)setResizeToFitText:(BOOL)resizeToFitText { } --(void)setTextAlignment:(MTLabelTextAlignment)textAlignment { - +- (void)setTextAlignment:(MTLabelTextAlignment)textAlignment { if (_textAlignment != textAlignment) { - _textAlignment = textAlignment; [self setNeedsDisplay]; - } - } @@ -180,7 +130,7 @@ -(void)setTextAlignment:(MTLabelTextAlignment)textAlignment { -(NSString *)text { - + return _text; } @@ -230,7 +180,7 @@ - (void)setup { self._textAlignment = MTLabelTextAlignmentLeft; [self setOpaque:NO]; } --(id)init { +- (id)init { self = [super init]; @@ -240,7 +190,7 @@ -(id)init { return self; } --(id)initWithFrame:(CGRect)frame { +- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; @@ -268,7 +218,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder { return self; } --(id)initWithText:(NSString *)text { +- (id)initWithText:(NSString *)text { self = [super init]; @@ -279,29 +229,29 @@ -(id)initWithText:(NSString *)text { return self; } -+(id)label { ++ (id)label { - return [[[MTLabel alloc] init] autorelease]; + return [[MTLabel alloc] init]; } -+(id)labelWithFrame:(CGRect)frame andText:(NSString *)text { ++ (id)labelWithFrame:(CGRect)frame andText:(NSString *)text { - return [[[MTLabel alloc] initWithFrame:frame andText:text] autorelease]; + return [[MTLabel alloc] initWithFrame:frame andText:text]; } -+(id)labelWithText:(NSString *)text { ++ (id)labelWithText:(NSString *)text { - return [[[MTLabel alloc] initWithText:text] autorelease]; + return [[MTLabel alloc] initWithText:text]; } #pragma mark - Drawing --(void)drawTransparentBackground { +- (void)drawTransparentBackground { [self setBackgroundColor:[UIColor clearColor]]; @@ -342,36 +292,31 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { } //Create a CoreText font object with name and size from the UIKit one - CTFontRef font = CTFontCreateWithName((CFStringRef)_font.fontName , - _font.pointSize, - NULL); + CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)_font.fontName, _font.pointSize, NULL); - //Setup the attributes dictionary with font and color NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: - (id)font, (id)kCTFontAttributeName, + (__bridge id)font, (id)kCTFontAttributeName, _fontColor.CGColor, kCTForegroundColorAttributeName, nil]; - NSAttributedString *attributedString = [[[NSAttributedString alloc] - initWithString:_text - attributes:attributes] autorelease]; + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:_text attributes:attributes]; CFRelease(font); - //Create a TypeSetter object with the attributed text created earlier on - CTTypesetterRef typeSetter = CTTypesetterCreateWithAttributedString((CFAttributedStringRef)attributedString); + CTTypesetterRef typeSetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString); //Start drawing from the upper side of view (the context is flipped, so we need to grab the height to do so) CGFloat y = self.bounds.origin.y + self.bounds.size.height - _font.ascender; BOOL shouldDrawAlong = YES; - int count = 0; CFIndex currentIndex = 0; _textHeight = 0; + _numberOfLines = 0; //Start drawing lines until we run out of text while (shouldDrawAlong) { + _numberOfLines++; //Get CoreText to suggest a proper place to place the line break CFIndex lineLength = CTTypesetterSuggestLineBreak(typeSetter, @@ -382,7 +327,17 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRange lineRange = CFRangeMake(currentIndex, lineLength); CTLineRef line = CTTypesetterCreateLine(typeSetter, lineRange); - //Create a new CTLine if we want to justify the text + if (_limitToNumberOfLines && _numberOfLines >= _maxNumberOfLines && currentIndex < [_text length]-3) { + shouldDrawAlong = NO; + + CFAttributedStringRef truncationString = CFAttributedStringCreate(NULL, CFSTR("\u2026"), (__bridge CFDictionaryRef)attributes); + CTLineRef truncationToken = CTLineCreateWithAttributedString(truncationString); + + CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width*.8, kCTLineTruncationEnd, truncationToken); + CFRelease(line); line = nil; + + line = truncatedLine; + } if (_textAlignment == MTLabelTextAlignmentJustify) { CTLineRef justifiedLine = CTLineCreateJustifiedLine(line, 1.0, self.bounds.size.width); @@ -396,7 +351,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { // Draw highlight if color has been set if (_fontHighlightColor != nil) { CGContextSetFillColorWithColor(context, _fontHighlightColor.CGColor); - CGRect lineRect = CTLineGetTypographicBoundsAsRect(line, CGPointMake(x, y));// + (self._lineHeight - self._font.pointSize) / 2)); + CGRect lineRect = CTLineGetTypographicBoundsAsRect(line, CGPointMake(x, y)); lineRect = CGRectIntegral(lineRect); lineRect = CGRectInset(lineRect, -1, -1); @@ -406,22 +361,14 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { substring = [substring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (0 < [substring length]) { - CGContextFillRect(context, lineRect); } } + //Setup the line position CGContextSetTextPosition(context, x, y); CTLineDraw(line, context); - - //Check to see if our index didn't exceed the text, and if should limit to number of lines - if ((currentIndex + lineLength >= [_text length]) && - !(_limitToNumberOfLines && count < _numberOfLines-1) ) { - shouldDrawAlong = NO; - - } - count++; CFRelease(line); CGFloat minFontSizeChange = 1; @@ -430,6 +377,10 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { currentIndex += lineLength; _textHeight += _lineHeight; + if (currentIndex >= [_text length]) { + shouldDrawAlong = NO; + } + if (_adjustSizeToFit && _font.pointSize > _minimumFontSize) { if (self.bounds.size.height < _textHeight) { @@ -473,7 +424,6 @@ - (void)drawRect:(CGRect)rect { [self drawTextInRect:rect inContext:context]; - if (_shouldResizeToFit && self.frame.size.height < _textHeight) { [self setFrame:CGRectMake(self.frame.origin.x, @@ -485,7 +435,7 @@ - (void)drawRect:(CGRect)rect { [delegate labelDidChangeFrame:self.frame]; // Ugly hack to avoid content being stretched - [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; +// [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; } CGContextRestoreGState(context); [super drawRect:self.bounds]; @@ -496,12 +446,6 @@ - (void)drawRect:(CGRect)rect { - (void)dealloc { - [_text release]; _text = nil; - [_fontColor release]; _fontColor = nil; - [_fontHighlightColor release], _fontHighlightColor = nil; - [_font release]; _font = nil; - - [super dealloc]; } @end From 7ad96b030d40f635a4e39a21a372102027a6246b Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Thu, 16 Aug 2012 17:54:10 -0400 Subject: [PATCH 02/18] Further fixes --- MTLabel/MTLabel.m | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 884d436..9acce20 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -177,7 +177,10 @@ - (void)setup { _textHeight = 0; self._font = [UIFont systemFontOfSize:DEFAULT_FONT_SIZE]; self._lineHeight = _font.lineHeight; - self._textAlignment = MTLabelTextAlignmentLeft; + self._textAlignment = MTLabelTextAlignmentLeft; + self.contentMode = UIViewContentModeRedraw; + self.contentStretch = CGRectMake(1, 1, 0, 0); + self.clipsToBounds = NO; [self setOpaque:NO]; } - (id)init { @@ -333,7 +336,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFAttributedStringRef truncationString = CFAttributedStringCreate(NULL, CFSTR("\u2026"), (__bridge CFDictionaryRef)attributes); CTLineRef truncationToken = CTLineCreateWithAttributedString(truncationString); - CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width*.8, kCTLineTruncationEnd, truncationToken); + CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width*.85, kCTLineTruncationEnd, truncationToken); CFRelease(line); line = nil; line = truncatedLine; @@ -405,7 +408,6 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { } CFRelease(typeSetter); - } - (void)drawRect:(CGRect)rect { @@ -431,11 +433,11 @@ - (void)drawRect:(CGRect)rect { self.frame.size.width, _textHeight)]; - // Notify delegate that we did change frame [delegate labelDidChangeFrame:self.frame]; // Ugly hack to avoid content being stretched -// [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; + [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; + } CGContextRestoreGState(context); [super drawRect:self.bounds]; From 1dd4ed931410359ab15c9e0eb9d4ab4d2e3fe7e3 Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Thu, 16 Aug 2012 18:14:19 -0400 Subject: [PATCH 03/18] Removed shadow --- MTLabel/MTLabel.h | 2 +- MTLabel/MTLabel.m | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/MTLabel/MTLabel.h b/MTLabel/MTLabel.h index cc80a89..28f4bba 100644 --- a/MTLabel/MTLabel.h +++ b/MTLabel/MTLabel.h @@ -39,7 +39,7 @@ typedef enum { @property (nonatomic, readonly) CGFloat _textHeight; @property (nonatomic, readwrite, setter = setMinimumFontSize:, getter = minimumFontSize) CGFloat _minimumFontSize; @property (nonatomic, retain, setter = setText:, getter = text) NSString *_text; -@property (nonatomic, retain, setter = setFontColor:, getter = fontColor) UIColor *_fontColor; +@property (nonatomic, retain, setter = setTextColor:, getter = fontColor) UIColor *_textColor; @property (nonatomic, retain, setter = setFontHighlightColor:, getter = fontHighlightColor) UIColor *_fontHighlightColor; @property (nonatomic, retain, setter = setFont:, getter = font) UIFont *_font; @property (nonatomic, readwrite, setter = setLimitToNumberOfLines:, getter = limitToNumberOfLines) BOOL _limitToNumberOfLines; diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 9acce20..05497d8 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -46,7 +46,7 @@ @implementation MTLabel @synthesize _lineHeight, _textHeight, _minimumFontSize; @synthesize _numberOfLines, _maxNumberOfLines; @synthesize _font; -@synthesize _fontColor, _fontHighlightColor; +@synthesize _textColor, _fontHighlightColor; @synthesize _limitToNumberOfLines, _shouldResizeToFit; @synthesize _textAlignment; @synthesize delegate; @@ -88,8 +88,8 @@ - (void)setFont:(UIFont *)font { - (void)setFontColor:(UIColor *)fontColor { - if (fontColor != _fontColor) { - _fontColor = fontColor; + if (fontColor != _textColor) { + _textColor = fontColor; [self setNeedsDisplay]; } @@ -153,7 +153,7 @@ -(int)numberOfLines { -(UIColor *)fontColor { - return _fontColor; + return _textColor; } -(BOOL)limitToNumberOfLines { @@ -299,7 +299,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)font, (id)kCTFontAttributeName, - _fontColor.CGColor, kCTForegroundColorAttributeName, + _textColor.CGColor, kCTForegroundColorAttributeName, nil]; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:_text attributes:attributes]; @@ -419,10 +419,6 @@ - (void)drawRect:(CGRect)rect { CGContextScaleCTM(context, 1.0, -1.0); CGContextSaveGState(context); - - CGColorRef colorRef = CGColorCreate(CGColorSpaceCreateDeviceRGB(), CGColorGetComponents([_fontColor CGColor])); - CGContextSetShadowWithColor(context, CGSizeMake(self.shadowOffset, self.shadowOffset), 5, colorRef); - CGColorRelease(colorRef); [self drawTextInRect:rect inContext:context]; From 997e3e6004bcda2f453ff7337c9a40471d1f8988 Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Fri, 17 Aug 2012 09:29:46 -0400 Subject: [PATCH 04/18] Code cleanup --- MTLabel/MTLabel.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 05497d8..fbf61b7 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -79,7 +79,7 @@ - (void)setText:(NSString *)text { - (void)setFont:(UIFont *)font { -if (font != _font) { + if (font != _font) { _font = font; self._lineHeight = _font.lineHeight; [self setNeedsDisplay]; From 79b8c64ad00361e88ad986f91db9c0297d8951e5 Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Fri, 17 Aug 2012 11:06:41 -0400 Subject: [PATCH 05/18] Code cleanup --- MTLabel/MTLabel.m | 1 - 1 file changed, 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index fbf61b7..5e49f8f 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -439,7 +439,6 @@ - (void)drawRect:(CGRect)rect { [super drawRect:self.bounds]; } - #pragma mark - Memory managment - (void)dealloc { From 6ec9d3daf087d0029740847c9ff152179b23141c Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Fri, 17 Aug 2012 12:05:14 -0400 Subject: [PATCH 06/18] Removed ugly hack --- MTLabel/MTLabel.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 5e49f8f..f84d24c 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -432,7 +432,7 @@ - (void)drawRect:(CGRect)rect { [delegate labelDidChangeFrame:self.frame]; // Ugly hack to avoid content being stretched - [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; + // [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; } CGContextRestoreGState(context); From 77967c3b73bfffc88270ae5774c25935c08740f4 Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Mon, 20 Aug 2012 17:47:30 -0400 Subject: [PATCH 07/18] MTLabel additions to help with layout, and to fix a bug that required us to manually redraw. --- MTLabel/MTLabel.h | 8 ++++++ MTLabel/MTLabel.m | 65 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/MTLabel/MTLabel.h b/MTLabel/MTLabel.h index 28f4bba..0669d3f 100644 --- a/MTLabel/MTLabel.h +++ b/MTLabel/MTLabel.h @@ -17,6 +17,9 @@ // limitations under the License. #import + +@class HAWLabel; + @protocol MTLabelDelegate - (void)labelDidChangeFrame:(CGRect)frame; @end @@ -72,5 +75,10 @@ typedef enum { -(BOOL)resizeToFitText; -(MTLabelTextAlignment)textAlignment; +- (CGFloat)labelBottomMargin; +- (CGFloat)labelTopMargin; +- (CGFloat)positionBelowLabel:(HAWLabel *)label offset:(CGSize)offset; +- (CGFloat)positionBelowView:(UIView *)view offset:(CGSize)offset; +- (CGFloat)positionBelowMTLabel:(MTLabel *)label offset:(CGSize)offset; @end diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index f84d24c..5d6f339 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -17,6 +17,8 @@ // limitations under the License. #import "MTLabel.h" +#import "HAWLabel.h" +#import "UILabel+HAWAdditions.h" #import #import @@ -166,6 +168,11 @@ -(BOOL)resizeToFitText { return _shouldResizeToFit; } +- (CGSize)sizeThatFits:(CGSize)size +{ + return CGSizeMake(self.frame.size.width, _textHeight); +} + -(MTLabelTextAlignment)textAlignment { return _textAlignment; @@ -410,7 +417,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRelease(typeSetter); } - (void)drawRect:(CGRect)rect { - + NSLog(@"Drawn: %@", self.text); CGContextRef context = UIGraphicsGetCurrentContext(); //Grab the drawing context and flip it to prevent drawing upside-down @@ -427,13 +434,11 @@ - (void)drawRect:(CGRect)rect { [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, - _textHeight)]; - - [delegate labelDidChangeFrame:self.frame]; - - // Ugly hack to avoid content being stretched - // [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0001]; + roundf(_textHeight))]; + dispatch_async(dispatch_get_current_queue(), ^{ + [self setNeedsDisplay]; + }); } CGContextRestoreGState(context); [super drawRect:self.bounds]; @@ -445,4 +450,50 @@ - (void)dealloc { } +#pragma mark - Fine positioning + +- (CGFloat)labelBottomMargin +{ + int lines = self.bounds.size.height / self.font.lineHeight; + if (lines > 1) + return -1.0 * self.font.descender + (self.font.lineHeight - self.font.pointSize); + else + return self.bounds.size.height - self.font.ascender; +} + +- (CGFloat)labelTopMargin +{ + return self.font.ascender - self.font.capHeight; +} + +- (CGFloat)positionBelowLabel:(HAWLabel *)label offset:(CGSize)offset +{ + CGRect fromRect = label.frame; + CGRect toRect = self.bounds; + toRect.origin.x = fromRect.origin.x + offset.width; + toRect.origin.y = roundf(fromRect.origin.y + fromRect.size.height - label.labelBottomMargin + offset.height - self.labelTopMargin); + self.frame = toRect; + return roundf(CGRectGetMaxY(toRect) - self.labelBottomMargin); +} + +- (CGFloat)positionBelowView:(UIView *)view offset:(CGSize)offset +{ + CGRect fromRect = view.frame; + CGRect toRect = self.bounds; + toRect.origin.x = fromRect.origin.x + offset.width; + toRect.origin.y = roundf(fromRect.origin.y + fromRect.size.height + offset.height - self.labelTopMargin); + self.frame = toRect; + return roundf(CGRectGetMaxY(toRect) - self.labelBottomMargin); +} + +- (CGFloat)positionBelowMTLabel:(MTLabel *)label offset:(CGSize)offset +{ + CGRect fromRect = label.frame; + CGRect toRect = self.bounds; + toRect.origin.x = fromRect.origin.x + offset.width; + toRect.origin.y = roundf(fromRect.origin.y + fromRect.size.height - label.labelBottomMargin + offset.height - self.labelTopMargin); + self.frame = toRect; + return roundf(CGRectGetMaxY(toRect) - self.labelBottomMargin); +} + @end From a337d3dd00ce74cc83631b761b3af32f6bd0b518 Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Tue, 21 Aug 2012 10:31:50 -0400 Subject: [PATCH 08/18] Implemented sizeThatFits to enable sizeToFit on MTLabels. --- MTLabel/MTLabel.m | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 5d6f339..9a35d1d 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -168,11 +168,6 @@ -(BOOL)resizeToFitText { return _shouldResizeToFit; } -- (CGSize)sizeThatFits:(CGSize)size -{ - return CGSizeMake(self.frame.size.width, _textHeight); -} - -(MTLabelTextAlignment)textAlignment { return _textAlignment; @@ -295,6 +290,42 @@ - (CGFloat)textOffsetForLine:(CTLineRef)line inRect:(CGRect)rect { return x; } + +- (CGSize)sizeThatFits:(CGSize)size +{ + CGFloat height = 0.0; + BOOL hasMoreText = YES; + + CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)_font.fontName, _font.pointSize, NULL); + NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: + (__bridge id)font, (id)kCTFontAttributeName, + _textColor.CGColor, kCTForegroundColorAttributeName, + nil]; + + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:_text attributes:attributes]; + CFRelease(font); + + CTTypesetterRef typeSetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString); + CFIndex currentIndex = 0; + int lineCount = 0; + + while (hasMoreText) { + CFIndex lineLength = CTTypesetterSuggestLineBreak(typeSetter, + currentIndex, + self.bounds.size.width); + currentIndex += lineLength; + height += _lineHeight; + lineCount++; + + if (currentIndex >= [_text length] || (_limitToNumberOfLines && lineCount >= _maxNumberOfLines)) { + hasMoreText = NO; + } + + } + + return CGSizeMake(self.bounds.size.width, height); +} + - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { if (!_text) { From 2615c06831eba023f4f86816426b9cfbb01cbd0a Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Tue, 21 Aug 2012 10:59:33 -0400 Subject: [PATCH 09/18] Added more utils for helping layout - can now access height of text instead of just frame. --- MTLabel/MTLabel.h | 1 + MTLabel/MTLabel.m | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/MTLabel/MTLabel.h b/MTLabel/MTLabel.h index 0669d3f..360ffda 100644 --- a/MTLabel/MTLabel.h +++ b/MTLabel/MTLabel.h @@ -80,5 +80,6 @@ typedef enum { - (CGFloat)positionBelowLabel:(HAWLabel *)label offset:(CGSize)offset; - (CGFloat)positionBelowView:(UIView *)view offset:(CGSize)offset; - (CGFloat)positionBelowMTLabel:(MTLabel *)label offset:(CGSize)offset; +- (CGFloat)layoutHeight; @end diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 9a35d1d..a97b149 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -448,7 +448,6 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRelease(typeSetter); } - (void)drawRect:(CGRect)rect { - NSLog(@"Drawn: %@", self.text); CGContextRef context = UIGraphicsGetCurrentContext(); //Grab the drawing context and flip it to prevent drawing upside-down @@ -527,4 +526,9 @@ - (CGFloat)positionBelowMTLabel:(MTLabel *)label offset:(CGSize)offset return roundf(CGRectGetMaxY(toRect) - self.labelBottomMargin); } +- (CGFloat)layoutHeight +{ + return self.frame.size.height - self.labelBottomMargin - self.labelTopMargin; +} + @end From 70239a3a8d7d5fec3594bcebcfb00af9f604da3b Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Tue, 21 Aug 2012 11:01:50 -0400 Subject: [PATCH 10/18] Fixed lack of rounding in layout height - causing blurred pixels due to non-whole pixel rendering. --- MTLabel/MTLabel.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index a97b149..2553b48 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -528,7 +528,7 @@ - (CGFloat)positionBelowMTLabel:(MTLabel *)label offset:(CGSize)offset - (CGFloat)layoutHeight { - return self.frame.size.height - self.labelBottomMargin - self.labelTopMargin; + return roundf(self.frame.size.height - self.labelBottomMargin - self.labelTopMargin); } @end From 6460653a64b684c6b301011eaacf54d304d01787 Mon Sep 17 00:00:00 2001 From: Brad Heintz Date: Tue, 21 Aug 2012 11:15:57 -0400 Subject: [PATCH 11/18] We do not need an NSLog for every piece of text that gets drawn. --- MTLabel/MTLabel.m | 1 - 1 file changed, 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 5d6f339..f89c0ac 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -417,7 +417,6 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRelease(typeSetter); } - (void)drawRect:(CGRect)rect { - NSLog(@"Drawn: %@", self.text); CGContextRef context = UIGraphicsGetCurrentContext(); //Grab the drawing context and flip it to prevent drawing upside-down From c4d1968289815ebdaf23b5f1bb9cae114a8d3f36 Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Tue, 21 Aug 2012 17:48:35 -0400 Subject: [PATCH 12/18] Fixed a missing nil-check that caused crashes when strings were nil --- MTLabel/MTLabel.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 2553b48..9489a72 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -293,6 +293,9 @@ - (CGFloat)textOffsetForLine:(CTLineRef)line inRect:(CGRect)rect { - (CGSize)sizeThatFits:(CGSize)size { + if (_text == nil) { + return CGSizeMake(self.bounds.size.width, 0.0); + } CGFloat height = 0.0; BOOL hasMoreText = YES; From 2a021bc566322ebd0fefc3a19269bef58bde7efb Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Wed, 5 Sep 2012 17:13:15 -0400 Subject: [PATCH 13/18] Memory Leak fix --- MTLabel/MTLabel.m | 1 + 1 file changed, 1 insertion(+) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 9489a72..dcf191f 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -376,6 +376,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFAttributedStringRef truncationString = CFAttributedStringCreate(NULL, CFSTR("\u2026"), (__bridge CFDictionaryRef)attributes); CTLineRef truncationToken = CTLineCreateWithAttributedString(truncationString); + CFRelease(truncationString); CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width*.85, kCTLineTruncationEnd, truncationToken); CFRelease(line); line = nil; From 4834c9d359ede35f837dd86ec01dd94a2f8d8503 Mon Sep 17 00:00:00 2001 From: James Paolantonio Date: Mon, 10 Sep 2012 10:44:40 -0400 Subject: [PATCH 14/18] Date Card Fix --- MTLabel/MTLabel.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index dcf191f..caedc03 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -324,6 +324,9 @@ - (CGSize)sizeThatFits:(CGSize)size hasMoreText = NO; } + if (_limitToNumberOfLines && lineCount >= _maxNumberOfLines) { + NSLog(@"BOOM ADD... - %@", _text); + } } return CGSizeMake(self.bounds.size.width, height); @@ -378,7 +381,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CTLineRef truncationToken = CTLineCreateWithAttributedString(truncationString); CFRelease(truncationString); - CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width*.85, kCTLineTruncationEnd, truncationToken); + CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width-40, kCTLineTruncationEnd, truncationToken); CFRelease(line); line = nil; line = truncatedLine; From d55e612432ae5decb9463b597371ab8228be9341 Mon Sep 17 00:00:00 2001 From: Brad Heintz Date: Tue, 11 Sep 2012 14:59:18 -0400 Subject: [PATCH 15/18] took out extraneous NSLog --- MTLabel/MTLabel.m | 4 ---- 1 file changed, 4 deletions(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index caedc03..a16aaf8 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -323,10 +323,6 @@ - (CGSize)sizeThatFits:(CGSize)size if (currentIndex >= [_text length] || (_limitToNumberOfLines && lineCount >= _maxNumberOfLines)) { hasMoreText = NO; } - - if (_limitToNumberOfLines && lineCount >= _maxNumberOfLines) { - NSLog(@"BOOM ADD... - %@", _text); - } } return CGSizeMake(self.bounds.size.width, height); From 9fec6628d1661c79e94b8b2c4c80913bae0c2680 Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Fri, 30 Nov 2012 10:27:57 -0500 Subject: [PATCH 16/18] Fix memory leak. --- MTLabel/MTLabel.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index a16aaf8..952c654 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -325,6 +325,8 @@ - (CGSize)sizeThatFits:(CGSize)size } } + CFRelease(typeSetter); + return CGSizeMake(self.bounds.size.width, height); } From 38ca15062362f4451a602ef9857f48004d5f52b4 Mon Sep 17 00:00:00 2001 From: Jerry Wong Date: Fri, 1 Feb 2013 11:30:00 -0500 Subject: [PATCH 17/18] Fix memory leak. --- MTLabel/MTLabel.m | 1 + 1 file changed, 1 insertion(+) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 952c654..dd17092 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -380,6 +380,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRelease(truncationString); CTLineRef truncatedLine = CTLineCreateTruncatedLine(line, self.bounds.size.width-40, kCTLineTruncationEnd, truncationToken); + CFRelease(truncationToken); CFRelease(line); line = nil; line = truncatedLine; From bb0f347b7e32ff08f3733eb73f7f18f910155b48 Mon Sep 17 00:00:00 2001 From: Carl Jahn Date: Wed, 24 Apr 2013 13:31:01 +0200 Subject: [PATCH 18/18] - updated gitignore - removed HAWLabel dependencies - Updated Project Settings - Added iPhone 5 Splashscreen - Fixed big that resetzt line height after font its setter --- .gitignore | 29 ++++++++++++++--------------- Default-568h@2x.png | Bin 0 -> 18594 bytes MTLabel.xcodeproj/project.pbxproj | 13 ++++++++++++- MTLabel/MTLabel.m | 8 +++++--- 4 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 Default-568h@2x.png diff --git a/.gitignore b/.gitignore index bb59993..20d4ce3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,18 +1,17 @@ -# xcode noise -*.mode1v3 +# Xcode +.DS_Store +*/build/* *.pbxuser -*.perspective +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 *.perspectivev3 -*.pyc -*~.nib/ -build/* - -# Textmate - if you build your xcode projects with it -*.tm_build_errors - -# old skool -.svn - -# osx noise -.DS_Store +!default.perspectivev3 +xcuserdata profile +*.moved-aside +DerivedData +.idea/ +*.hmap \ No newline at end of file diff --git a/Default-568h@2x.png b/Default-568h@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..0891b7aabfcf3422423b109c8beed2bab838c607 GIT binary patch literal 18594 zcmeI4X;f257Jx&9fS`ixvS;&$x8J@slQFSel)6zJN=?13FB7H(lQjRkSy8x_-S~tvu2gzn1oS+dLcF#eqtq$ z%tf9TTvX?`)R@}3uBI;jzS-=ZR-Td&MHaS&;!0?Ni*#$#`n*~CcQK)Q9vAQ~TUpnI!j)a2biYK^R)M~A5wUDZhx?ULMX z3x1P&qt=trOY6P2U67L=m=U?F|5#Uj(eCueNTZaHs_ceWiHeET+j+tp3Jt9g(ekqP z2WOvfR{qV+9r+o4J5?qK>7;;^+I7tGv-i)es$X_D=EoKF+S?zsyj^oRFElP}c}JT< zd8SUs-?O?}2YD#ngKbnHgzHBcboxK_2r9l(?eNCl-pEzkJm}fY?WC*jnS?VBE4EpY zO$fEejz6fU;W2Kl>JeQBZBl-%Irg`obSlg*@4QB;Dd1H7^Oi5wvt4d{RZ!8Og?^aE z)k0$1g+V3fd(gdQ3d&q2q-FL*uy#}|bc^=VhFsl0jBgUGJ+-s3U8MK9A!YJJMxpci z5hJ%|{DwV48fZn0{n5l$N_KcSb#NKE4plB`9I6Zt=Z!~-zw0{9tg$L&Ju1F0X)Cy8 zKF;(&lJ>x)Jw(=;p~sF(Sd9VWGwFE2rnyS9!f^DZ8+aCLq zQ};>lcJ1GDLqjm6Hd>|Eabno@P`~Bn(~6^aD_#yoEH(a?Nm1S<;S+hSxI5d16^<1lEM3NPFi zkqPrpL)+ zgnseFikg`gJVBha1&7C4;O6>h=dt~`ND+;Zd?W(4v2JIb7Pt>Td42%M-Ju-XAH#Pns762L}K3 zDhvsRqN0Ni(1UrishD2YvV?4*h2iFj$+&N||Fn$4n|^NSU+o?~jq`0jVQt8T9l{7b zXiwwODFh2V!Q6sqP9S>WH$oOf$N~=d0-bqTlD61!=`&0eAP-F>XN?*|gtOXX{ zQVTWyYo4ZK0GAw!GHf|pz9`D;-bbb*5LBX*{bnz|+)$@&P9|ORM2o?95{;ejvo&r- zq8cBhTN6nn)7~W>54U)%-F_-b?YKdfk5I8MHcuzBD5)!;yv#Z&R&^y=@=>VTIMy#r zX&U<=BsPkdqcMe<_}2+>H%XKyrr5ZR8_KVe>ZqYN z^=^~TFD};;rHJ$U;{~w^hYojl4hRI@SH$^K{YEo=sg)WY87r!*7blQK&qnpDo0`Vn zkl)9u9g=mCh&ZCJS(L4yN3k0kQ zuvg$h2KEEk51T+O0JQ+r0`R>g{jvqM0Mr6d3qUOZwE!?PI7HY@CE|dr sfw?Q;rAv?G4&^^8-z_>&sWXMxvD*gPOU4CBe-*@OtE+wfmVJNyHv)PfH~;_u literal 0 HcmV?d00001 diff --git a/MTLabel.xcodeproj/project.pbxproj b/MTLabel.xcodeproj/project.pbxproj index 07f9928..8186047 100644 --- a/MTLabel.xcodeproj/project.pbxproj +++ b/MTLabel.xcodeproj/project.pbxproj @@ -16,8 +16,9 @@ 147737E11391B7C200BCB59A /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 147737DF1391B7C200BCB59A /* MainWindow.xib */; }; 147737E41391B7C200BCB59A /* MTLabelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 147737E31391B7C200BCB59A /* MTLabelViewController.m */; }; 147737E71391B7C200BCB59A /* MTLabelViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 147737E51391B7C200BCB59A /* MTLabelViewController.xib */; }; - 147737EF1391B7D400BCB59A /* MTLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 147737EE1391B7D400BCB59A /* MTLabel.m */; }; + 147737EF1391B7D400BCB59A /* MTLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 147737EE1391B7D400BCB59A /* MTLabel.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; 147737F11391B86600BCB59A /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 147737F01391B86600BCB59A /* CoreText.framework */; }; + FA614AF41727F8FC00724B20 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FA614AF31727F8FC00724B20 /* Default-568h@2x.png */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -38,6 +39,7 @@ 147737ED1391B7D400BCB59A /* MTLabel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTLabel.h; sourceTree = ""; }; 147737EE1391B7D400BCB59A /* MTLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTLabel.m; sourceTree = ""; }; 147737F01391B86600BCB59A /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; }; + FA614AF31727F8FC00724B20 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +60,7 @@ 147737BE1391B7C100BCB59A = { isa = PBXGroup; children = ( + FA614AF31727F8FC00724B20 /* Default-568h@2x.png */, 147737D31391B7C100BCB59A /* MTLabel */, 147737CC1391B7C100BCB59A /* Frameworks */, 147737CA1391B7C100BCB59A /* Products */, @@ -135,6 +138,9 @@ /* Begin PBXProject section */ 147737C01391B7C100BCB59A /* Project object */ = { isa = PBXProject; + attributes = { + LastUpgradeCheck = 0460; + }; buildConfigurationList = 147737C31391B7C100BCB59A /* Build configuration list for PBXProject "MTLabel" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; @@ -160,6 +166,7 @@ 147737D81391B7C200BCB59A /* InfoPlist.strings in Resources */, 147737E11391B7C200BCB59A /* MainWindow.xib in Resources */, 147737E71391B7C200BCB59A /* MTLabelViewController.xib in Resources */, + FA614AF41727F8FC00724B20 /* Default-568h@2x.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -247,7 +254,9 @@ GCC_DYNAMIC_NO_PIC = NO; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "MTLabel/MTLabel-Prefix.pch"; + GCC_VERSION = ""; INFOPLIST_FILE = "MTLabel/MTLabel-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -260,7 +269,9 @@ COPY_PHASE_STRIP = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "MTLabel/MTLabel-Prefix.pch"; + GCC_VERSION = ""; INFOPLIST_FILE = "MTLabel/MTLabel-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; PRODUCT_NAME = "$(TARGET_NAME)"; VALIDATE_PRODUCT = YES; WRAPPER_EXTENSION = app; diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index dd17092..e73cf7b 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -17,8 +17,6 @@ // limitations under the License. #import "MTLabel.h" -#import "HAWLabel.h" -#import "UILabel+HAWAdditions.h" #import #import @@ -83,7 +81,11 @@ - (void)setText:(NSString *)text { - (void)setFont:(UIFont *)font { if (font != _font) { _font = font; + + if (self._lineHeight == [UIFont systemFontOfSize:DEFAULT_FONT_SIZE].lineHeight) { self._lineHeight = _font.lineHeight; + + } [self setNeedsDisplay]; } } @@ -502,7 +504,7 @@ - (CGFloat)labelTopMargin return self.font.ascender - self.font.capHeight; } -- (CGFloat)positionBelowLabel:(HAWLabel *)label offset:(CGSize)offset +- (CGFloat)positionBelowLabel:(MTLabel *)label offset:(CGSize)offset { CGRect fromRect = label.frame; CGRect toRect = self.bounds;