美文网首页
根据计算字符串宽度,字符串高度

根据计算字符串宽度,字符串高度

作者: 月沉眠love | 来源:发表于2019-07-19 17:36 被阅读0次

根据字符串计算宽度

//根据字符串计算宽度
-(CGFloat)WidthWithString:(NSString*)string fontSize:(CGFloat)fontSize height:(CGFloat)height
{
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    return  [string boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.width;
}

获得字符串的高度

//获得字符串的高度
+(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width
{
    CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping];//此处的换行类型(lineBreakMode)可根据自己的实际情况进行设置
    return sizeToFit.height;
}

相关文章

网友评论

      本文标题:根据计算字符串宽度,字符串高度

      本文链接:https://www.haomeiwen.com/subject/paqclctx.html