Skip to content

Instantly share code, notes, and snippets.

@eniton
Forked from adeel/UILabel+withString.h
Created May 13, 2012 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eniton/2688428 to your computer and use it in GitHub Desktop.
Save eniton/2688428 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