计算字符串的长度和高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//获取字符串的宽度
+(float) widthForString:(NSString *)string fontSize:(float)fontSize{
NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:17]};
CGSize size=[string sizeWithAttributes:attrs];
return size.width;
}
//获得字符串的高
+(float) heightForString:(NSString *)string fontSize:(float)fontSize{
NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:17]};
CGSize size=[string sizeWithAttributes:attrs];
return size.height;
}
iOS 根据文字的多少计算label宽度
NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:17]}; CGSize size=[_itemNamelabel.text sizeWithAttributes:attrs]; [self.itemNamelabel setFrame:CGRectMake(40, 10, size.width, 20)];