Skip to content

Instantly share code, notes, and snippets.

@adeel
Created July 21, 2011 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adeel/1098401 to your computer and use it in GitHub Desktop.
Save adeel/1098401 to your computer and use it in GitHub Desktop.
UILabel category that adds a method to easily make labels on the fly.
//
// UILabel+withString.h
//
#import <Foundation/Foundation.h>
@interface UILabel (withString)
+ (UILabel *)labelWithString:(NSString *)string
font:(UIFont *)font
color:(UIColor *)color
container:(CGRect)container
origin:(CGPoint)origin;
@end
//
// UILabel+withString.m
//
#import "UILabel+withString.h"
@implementation UILabel (withString)
+ (UILabel *)labelWithString:(NSString *)string
font:(UIFont *)font
color:(UIColor *)color
container:(CGRect)container
origin:(CGPoint)origin {
CGSize size = [string sizeWithFont:font constrainedToSize:container.size
lineBreakMode:UILineBreakModeTailTruncation];
UILabel *label = [[[UILabel alloc]
initWithFrame:CGRectMake(origin.x, origin.y, container.size.width, size.height)] autorelease];
label.text = string;
label.font = font;
label.textColor = color;
label.textAlignment = UITextAlignmentLeft;
label.numberOfLines = 1;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.backgroundColor = [UIColor clearColor];
return label;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment