Created
October 23, 2013 13:59
-
-
Save mindbrix/7119305 to your computer and use it in GitHub Desktop.
Support for creating a multi-line UILabel from NSAttributedStrings
This file contains hidden or 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
// | |
// 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 |
This file contains hidden or 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
// | |
// 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