youngshook
9/30/2014 - 7:06 AM

UILabel+MultiLineSupport.h

//
//  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