Created
January 13, 2014 18:21
-
-
Save TosinAF/8405316 to your computer and use it in GitHub Desktop.
My Subclass of UILabel to ensure that when a new excuses is displayed in my app, Procuses (tosinaf.github.io/Procuses), the height is dynamically updated to fit the new text content while keeping the width constant.
This file contains 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
// | |
// PEExcuseLabel.m | |
// Procuses | |
// | |
// Created by Tosin Afolabi on 04/08/2013. | |
// Copyright (c) 2013 Tosin Afolabi. All rights reserved. | |
// | |
#define IOS_NEWER_OR_EQUAL_TO_7 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 7.0 ) | |
#import "PEExcuseLabel.h" | |
@implementation PEExcuseLabel | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// Properties that all Excuse Labels Should Have | |
[self setTextAlignment:NSTextAlignmentCenter]; | |
[self setNumberOfLines:0]; | |
} | |
return self; | |
} | |
- (void)setExcuse:(NSString *)excuse | |
{ | |
[self setText:excuse]; | |
// Get Expected Size of String & Adjust Size of Label to Fit | |
CGSize maximumLabelSize = CGSizeMake(280,200); | |
CGSize expectedLabelSize = [self text:excuse sizeWithFont:self.font constrainedToSize:maximumLabelSize]; | |
CGRect newFrame = self.frame; | |
newFrame.size.width = 280.0; | |
newFrame.size.height = expectedLabelSize.height + 20; | |
self.frame = newFrame; | |
} | |
- (CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size | |
{ | |
if(IOS_NEWER_OR_EQUAL_TO_7){ | |
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: | |
font, NSFontAttributeName, | |
nil]; | |
CGRect frame = [text boundingRectWithSize:size | |
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) | |
attributes:attributesDictionary | |
context:nil]; | |
return frame.size; | |
}else{ | |
return [text sizeWithFont:font constrainedToSize:size]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment