Skip to content

Instantly share code, notes, and snippets.

@mindbrix
Created October 23, 2013 13:59
Show Gist options
  • Save mindbrix/7119305 to your computer and use it in GitHub Desktop.
Save mindbrix/7119305 to your computer and use it in GitHub Desktop.
Support for creating a multi-line UILabel from NSAttributedStrings
//
// UILabel+MultiLineSupport.h
// giffgaffIOS
//
// Created by Nigel Barber on 23/10/2013.
// Copyright (c) 2013 confidence. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UILabel (MultiLineSupport)
-(void)setMultiLineAttributedLabel:(NSArray *)attributedStrings;
-(CGSize)sizeForAttributedLabelWithConstraint:(CGSize)constraint;
@end
//
// UILabel+MultiLineSupport.m
// giffgaffIOS
//
// Created by Nigel Barber on 23/10/2013.
// Copyright (c) 2013 confidence. All rights reserved.
//
#import "UILabel+MultiLineSupport.h"
@implementation UILabel (MultiLineSupport)
-(void)setMultiLineAttributedLabel:(NSArray *)attributedStrings
{
NSParameterAssert( attributedStrings );
self.numberOfLines = 0;
self.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableAttributedString *labelAttributedString = [[ NSMutableAttributedString alloc ] init ];
for( NSAttributedString *attributedString in attributedStrings )
{
[ labelAttributedString appendAttributedString:attributedString ];
}
self.attributedText = labelAttributedString;
}
-(CGSize)sizeForAttributedLabelWithConstraint:(CGSize)constraint
{
if( self.attributedText )
{
CGRect boundingRect = [ self.attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil ];
return boundingRect.size;
}
return CGSizeZero;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment