From 34ab117399e36b95204f7b896b5511e28fac75fd Mon Sep 17 00:00:00 2001 From: Martin Hwasser Date: Tue, 6 Sep 2011 13:47:59 +0200 Subject: [PATCH 1/5] Fixes streching when the label is resized. --- MTLabel/MTLabel.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 39c20ea..23e0eb4 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -226,6 +226,7 @@ - (void)setup { self._font = [UIFont systemFontOfSize:DEFAULT_FONT_SIZE]; self._lineHeight = _font.lineHeight; self._textAlignment = MTLabelTextAlignmentLeft; + self.contentMode = UIViewContentModeRedraw; [self setOpaque:NO]; } -(id)init { @@ -478,8 +479,8 @@ - (void)drawRect:(CGRect)rect { // 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]; + // Redraw in the new bounds + [self setNeedsDisplayInRect:self.bounds]; } CGContextRestoreGState(context); [super drawRect:self.bounds]; From e9f2141650d05e23b081f05e5ade355e1f3d0edb Mon Sep 17 00:00:00 2001 From: Martin Hwasser Date: Tue, 6 Sep 2011 13:57:34 +0200 Subject: [PATCH 2/5] Updated .gitignore with xcuserdata (Xcode 4) --- .gitignore | 2 ++ MTLabel.xcodeproj/project.pbxproj | 3 +++ 2 files changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index bb59993..a501395 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ *.pyc *~.nib/ build/* +# xcode 4 +xcuserdata # Textmate - if you build your xcode projects with it *.tm_build_errors diff --git a/MTLabel.xcodeproj/project.pbxproj b/MTLabel.xcodeproj/project.pbxproj index 07f9928..6fe0283 100644 --- a/MTLabel.xcodeproj/project.pbxproj +++ b/MTLabel.xcodeproj/project.pbxproj @@ -135,6 +135,9 @@ /* Begin PBXProject section */ 147737C01391B7C100BCB59A /* Project object */ = { isa = PBXProject; + attributes = { + LastUpgradeCheck = 0420; + }; buildConfigurationList = 147737C31391B7C100BCB59A /* Build configuration list for PBXProject "MTLabel" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; From 3481d32d0494aefa4707de6ae61538c37769f508 Mon Sep 17 00:00:00 2001 From: Martin Hwasser Date: Tue, 4 Oct 2011 13:14:33 +0200 Subject: [PATCH 3/5] Some minor fixes and improvements. --- MTLabel/MTLabel.m | 63 ++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index 23e0eb4..a701909 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -22,7 +22,7 @@ #define DEFAULT_FONT_SIZE 12 -@interface MTLabel () +@interface MTLabel () -(void)drawTransparentBackground; @@ -51,7 +51,6 @@ @implementation MTLabel @synthesize _textAlignment; @synthesize delegate; @synthesize _adjustSizeToFit; - #pragma mark - Setters -(void)setNumberOfLines:(int)numberOfLines { @@ -85,6 +84,7 @@ -(void)setText:(NSString *)text { _text = nil; } + //_text = [[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain]; _text = [text retain]; [self setNeedsDisplay]; @@ -156,6 +156,7 @@ -(void)setResizeToFitText:(BOOL)resizeToFitText { if (_shouldResizeToFit != resizeToFitText) { _shouldResizeToFit = resizeToFitText; + self.clipsToBounds = NO; [self setNeedsDisplay]; } @@ -334,15 +335,17 @@ - (CGFloat)textOffsetForLine:(CTLineRef)line inRect:(CGRect)rect { return x; } -- (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { +- (void)drawTextInRect:(CGRect)rect withFont:(UIFont *)aFont lineHeight:(CGFloat)lineHeight inContext:(CGContextRef)context { if (!_text) { return; } + CGContextClearRect(context, rect); + //Create a CoreText font object with name and size from the UIKit one - CTFontRef font = CTFontCreateWithName((CFStringRef)_font.fontName , - _font.pointSize, + CTFontRef font = CTFontCreateWithName((CFStringRef)aFont.fontName , + aFont.pointSize, NULL); //Setup the attributes dictionary with font and color @@ -361,7 +364,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CTTypesetterRef typeSetter = CTTypesetterCreateWithAttributedString((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; + CGFloat y = rect.origin.y + rect.size.height - aFont.ascender; BOOL shouldDrawAlong = YES; int count = 0; @@ -375,7 +378,7 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { //Get CoreText to suggest a proper place to place the line break CFIndex lineLength = CTTypesetterSuggestLineBreak(typeSetter, currentIndex, - self.bounds.size.width); + rect.size.width); //Create a new line with from current index to line-break index CFRange lineRange = CFRangeMake(currentIndex, lineLength); @@ -384,13 +387,13 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { //Create a new CTLine if we want to justify the text if (_textAlignment == MTLabelTextAlignmentJustify) { - CTLineRef justifiedLine = CTLineCreateJustifiedLine(line, 1.0, self.bounds.size.width); + CTLineRef justifiedLine = CTLineCreateJustifiedLine(line, 1.0, rect.size.width); CFRelease(line); line = nil; line = justifiedLine; } - CGFloat x = [self textOffsetForLine:line inRect:self.bounds]; + CGFloat x = [self textOffsetForLine:line inRect:rect]; // Draw highlight if color has been set if (_fontHighlightColor != nil) { @@ -424,30 +427,30 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRelease(line); CGFloat minFontSizeChange = 1; - y -= _lineHeight; + y -= lineHeight; currentIndex += lineLength; - _textHeight += _lineHeight; + _textHeight += lineHeight; - if (_adjustSizeToFit && _font.pointSize > _minimumFontSize) { + if (_adjustSizeToFit && aFont.pointSize > _minimumFontSize) { - if (self.bounds.size.height < _textHeight) { + if (rect.size.height < _textHeight) { - NSString *fontName = _font.fontName; - CGFloat pointSize = _font.pointSize; - CGFloat lineHeightRatio = self._lineHeight / pointSize; + NSString *fontName = aFont.fontName; + CGFloat pointSize = aFont.pointSize; + CGFloat lineHeightRatio = lineHeight / pointSize; CGFloat newPointSize = pointSize - minFontSizeChange; // Make sure newPointSize is not less than the _minimumFontSize newPointSize = newPointSize < _minimumFontSize ? _minimumFontSize : newPointSize; - self._font = [UIFont fontWithName:fontName size:newPointSize]; - self._lineHeight = roundf(newPointSize * lineHeightRatio); + UIFont *newFont = [UIFont fontWithName:fontName size:newPointSize]; + CGFloat newLineHeight = roundf(newPointSize * lineHeightRatio); - CGContextClearRect(context, self.bounds); + CGContextClearRect(context, rect); CFRelease(typeSetter); - return [self drawTextInRect:self.bounds inContext:context]; + return [self drawTextInRect:rect withFont:newFont lineHeight:newLineHeight inContext:context]; } } } @@ -455,8 +458,10 @@ - (void)drawTextInRect:(CGRect)rect inContext:(CGContextRef)context { CFRelease(typeSetter); } -- (void)drawRect:(CGRect)rect { +- (void)drawRect:(CGRect)rect { + BOOL wasHidden = [self isHidden]; + [self setHidden:YES]; CGContextRef context = UIGraphicsGetCurrentContext(); //Grab the drawing context and flip it to prevent drawing upside-down @@ -466,11 +471,11 @@ - (void)drawRect:(CGRect)rect { CGContextSaveGState(context); - [self drawTextInRect:rect inContext:context]; - - + [self drawTextInRect:rect withFont:self._font lineHeight:self._lineHeight inContext:context]; + if (_shouldResizeToFit && self.frame.size.height < _textHeight) { - + + [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, @@ -480,10 +485,13 @@ - (void)drawRect:(CGRect)rect { [delegate labelDidChangeFrame:self.frame]; // Redraw in the new bounds - [self setNeedsDisplayInRect:self.bounds]; + [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.000001]; + } CGContextRestoreGState(context); - [super drawRect:self.bounds]; + + [super drawRect:rect]; + [self setHidden:wasHidden]; } @@ -495,7 +503,6 @@ - (void)dealloc { [_fontColor release]; _fontColor = nil; [_fontHighlightColor release], _fontHighlightColor = nil; [_font release]; _font = nil; - [super dealloc]; } From 30dde4a2e65921eb258f0a30970157cc84ac3168 Mon Sep 17 00:00:00 2001 From: Martin Hwasser Date: Sat, 5 Nov 2011 11:59:54 +0100 Subject: [PATCH 4/5] Some fixes. --- MTLabel/MTLabel.m | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index a701909..c0e10bf 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -28,7 +28,7 @@ -(void)drawTransparentBackground; @end -CGRect CTLineGetTypographicBoundsAsRect(CTLineRef line, CGPoint lineOrigin) { +CGRect CTLineGetTypographicBoundsAsARect(CTLineRef line, CGPoint lineOrigin) { CGFloat ascent = 0; CGFloat descent = 0; CGFloat leading = 0; @@ -228,6 +228,7 @@ - (void)setup { self._lineHeight = _font.lineHeight; self._textAlignment = MTLabelTextAlignmentLeft; self.contentMode = UIViewContentModeRedraw; + self.contentStretch = CGRectMake(1, 1, 0, 0); [self setOpaque:NO]; } -(id)init { @@ -398,7 +399,7 @@ - (void)drawTextInRect:(CGRect)rect withFont:(UIFont *)aFont lineHeight:(CGFloat // 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 = CTLineGetTypographicBoundsAsARect(line, CGPointMake(x, y));// + (self._lineHeight - self._font.pointSize) / 2)); lineRect = CGRectIntegral(lineRect); lineRect = CGRectInset(lineRect, -1, -1); @@ -431,7 +432,7 @@ - (void)drawTextInRect:(CGRect)rect withFont:(UIFont *)aFont lineHeight:(CGFloat currentIndex += lineLength; _textHeight += lineHeight; - + if (_adjustSizeToFit && aFont.pointSize > _minimumFontSize) { if (rect.size.height < _textHeight) { @@ -447,12 +448,15 @@ - (void)drawTextInRect:(CGRect)rect withFont:(UIFont *)aFont lineHeight:(CGFloat UIFont *newFont = [UIFont fontWithName:fontName size:newPointSize]; CGFloat newLineHeight = roundf(newPointSize * lineHeightRatio); + self._font = newFont; + self._lineHeight = newLineHeight; + CGContextClearRect(context, rect); CFRelease(typeSetter); return [self drawTextInRect:rect withFont:newFont lineHeight:newLineHeight inContext:context]; } - } + } } CFRelease(typeSetter); @@ -460,8 +464,7 @@ - (void)drawTextInRect:(CGRect)rect withFont:(UIFont *)aFont lineHeight:(CGFloat } - (void)drawRect:(CGRect)rect { - BOOL wasHidden = [self isHidden]; - [self setHidden:YES]; + CGContextRef context = UIGraphicsGetCurrentContext(); //Grab the drawing context and flip it to prevent drawing upside-down @@ -470,28 +473,39 @@ - (void)drawRect:(CGRect)rect { CGContextScaleCTM(context, 1.0, -1.0); CGContextSaveGState(context); - + [self drawTextInRect:rect withFont:self._font lineHeight:self._lineHeight inContext:context]; - if (_shouldResizeToFit && self.frame.size.height < _textHeight) { + if (_shouldResizeToFit && rect.size.height < _textHeight) { - [self setFrame:CGRectMake(self.frame.origin.x, - self.frame.origin.y, - self.frame.size.width, - _textHeight)]; + CGRect newFrame = CGRectMake(self.frame.origin.x, + self.frame.origin.y, + self.frame.size.width, + _textHeight); + [self setFrame:newFrame]; // Notify delegate that we did change frame [delegate labelDidChangeFrame:self.frame]; // Redraw in the new bounds + //[self setNeedsDisplayInRect:newFrame]; + //[self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.000001]; + + CGRect newRect = rect; + newRect.size.height = _textHeight; + CGContextClearRect(context, newRect); + + CGContextRestoreGState(context); + //[self drawTextInRect:newRect withFont:self._font lineHeight:self._lineHeight inContext:context]; [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.000001]; + //[self drawRect:newRect]; + } else { + CGContextRestoreGState(context); + + [super drawRect:rect]; } - CGContextRestoreGState(context); - - [super drawRect:rect]; - [self setHidden:wasHidden]; } From b1ef7eb68b0741b9888b4cc31c875533589ac6b0 Mon Sep 17 00:00:00 2001 From: Martin Hwasser Date: Mon, 27 May 2013 22:18:25 +0200 Subject: [PATCH 5/5] Removes deprecated method. --- MTLabel/MTLabel.m | 1 - 1 file changed, 1 deletion(-) diff --git a/MTLabel/MTLabel.m b/MTLabel/MTLabel.m index c0e10bf..b191ec5 100644 --- a/MTLabel/MTLabel.m +++ b/MTLabel/MTLabel.m @@ -228,7 +228,6 @@ - (void)setup { self._lineHeight = _font.lineHeight; self._textAlignment = MTLabelTextAlignmentLeft; self.contentMode = UIViewContentModeRedraw; - self.contentStretch = CGRectMake(1, 1, 0, 0); [self setOpaque:NO]; } -(id)init {