首页 > 学院 > 开发设计 > 正文

在UILabel内计算内容的大小

2019-11-14 20:35:52
字体:
来源:转载
供稿:网友

//  UILabel+LM.h

////  UILabel+LM.h//  Test////  Created by 李蒙 on 14-3-21.//  Copyright (c) 2014年 datangcloud. All rights reserved.//#import <UIKit/UIKit.h>@interface UILabel (LM)- (CGSize)contentSize;@end

 

//  UILabel+LM.m

////  UILabel+LM.m//  Test////  Created by 李蒙 on 14-3-21.//  Copyright (c) 2014年 datangcloud. All rights reserved.//#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

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表