標籤:uilabel 自適應高度
.h檔案
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface HeightWithString : NSObject
//計算各種文字高度
//參數1:文本 參數2:label的寬度 參數3:字型大小
+(CGFloat)heightForTextLable:(NSString *)text width:(CGFloat)width fontSize:(CGFloat)fontSize;
@end
.m 檔案
#import "HeightWithString.h"
@implementation HeightWithString
//計算文字高度
+(CGFloat)heightForTextLable:(NSString *)text width:(CGFloat)width fontSize:(CGFloat)fontSize
{ // 第一個參數:寬度,高度,高度一般越大越好
// 第二個參數:計算的類型
// 第三個參數:字型的大小(label字型設定和這個參數一定同意)
// 第四個參數:nil;
CGRect rect =[text boundingRectWithSize:CGSizeMake(width, 3000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize]} context:nil];
// NSLog(@"高度 = %.1f",rect.size.height);
return rect.size.height;
}
@end
本文出自 “ios” 部落格,請務必保留此出處http://10136044.blog.51cto.com/10126044/1679362
iOS UILabel的輸出自適應高度設定