现在的位置: 首页 > 综合 > 正文

UILabel Category 计算UILabel内容大小

2017年12月09日 ⁄ 综合 ⁄ 共 676字 ⁄ 字号 评论关闭
UILabel+ContentSize.h
#import <UIKit/UIKit.h>
@interface UILabel (ContentSize)
- (CGSize)contentSize;
@end

UILabel+ContentSize.m
#import "UILabel+ContentSize.h"
@implementation UILabel (ContentSize)
 
- (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;
}

抱歉!评论已关闭.