/// 文本字号计算 指定单行字符串高度
/// @param height 返回单行可显示的最大字号
-(CGFloat)fontSizeFromHeight:(CGFloat)height
{
CGFloat fontSize = 0;
for (int i = 0; i < (int)height ; i++)
{
CGFloat h = [self strHeightFromWidth:0 font:[UIFont systemFontOfSize:i] str:@"高"];
if (height - h >= 0)
{
fontSize = i;
}
}
return fontSize;
}
/// 字符串高度计算
/// @param width 指定宽度
/// @param font 指定字体
/// @param str 指定字符串
-(CGFloat)strHeightFromWidth:(CGFloat)width font:(UIFont *)font str:(NSString *)str
{
CGSize baseSize = CGSizeMake(width, CGFLOAT_MAX);
if (!font)
{
font = [UIFont systemFontOfSize:16.0f];
}
CGSize labelsize = [str boundingRectWithSize:baseSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size;
return labelsize.height;
}
/// 字符串宽度计算
/// @param heigh 指定高度
/// @param font 指定字体
/// @param str 指定字符串
-(CGFloat)strWidthtFromHeigh:(CGFloat)heigh font:(UIFont *)font str:(NSString *)str
{
CGSize baseSize = CGSizeMake( CGFLOAT_MAX , heigh);
if (!font)
{
font = [UIFont systemFontOfSize:16.0f];
}
CGSize labelsize = [str boundingRectWithSize:baseSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil].size;
return labelsize.width;
}