方法一:只有字號(hào)限制
NSString *string = @"計(jì)算文字長(zhǎng)度";
CGSize fontSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName]];
height:16.707031,width:85.680000
方法二:固定寬度計(jì)算字符串高度赎婚,常用于計(jì)算cell的高度
NSString *string = @"計(jì)算文字長(zhǎng)度計(jì)算文字長(zhǎng)度計(jì)算文字長(zhǎng)度計(jì)算文字長(zhǎng)度計(jì)算文字長(zhǎng)度計(jì)算文字長(zhǎng)度計(jì)算文字長(zhǎng)度";
CGRect fontRect = [string boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
NSLog(@"Width:%f,Height:%f",fontRect.size.width,fontRect.size.height);
Width:99.960000,Height:100.242188
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者