UILabelにコンテンツのサイズを計算する
1.UILable+LM.h
#import <UIKit/UIKit.h>
@interface UILabel (LM)
– (CGSize)contentSize;
@end
2.UILabel+LM.m
#import “UILabel+LM.h"
@implementation UILabel (LM)
– (CGSize)contentSize
{
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = self.lineBreakMode;
paragraphStyle.alignment = self.textAlignment;
NSDictionary * attributes = @{NSFontAttributeName : self.font,
NSParagraphStyleAttributeName : paragraphStyle};
CGSize contentSize = [self.text boundingRectWithSize:self.frame.size
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil].size;
return contentSize;
}
@end