首页 > 学院 > 开发设计 > 正文

获取字符串高度或宽度

2019-11-07 23:46:16
字体:
来源:转载
供稿:网友
//获取字符串高度-(CGFloat)getStringHighInputWidth:(NSInteger)number fontSize:(NSInteger)sizeFont{    //    第一个参数:宽度,高度,高度一般越大越好    //    第二个参数:计算的类型    //    第三个参数:字体的大小(label字体设置和这个参数一定同意)    //    第四个参数:nil;    CGRect rect =[self boundingRectWithSize:CGSizeMake(number, 3000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:sizeFont]} context:nil];    return rect.size.height;}

//获取字符串宽度-(CGFloat)getStringWidthFontSize:(NSInteger)sizeFont{    if ([[[UIDevice currentDevice] systemVersion] doubleValue] <= 7.0) {        CGSize size = [self sizeWithFont:[UIFont boldSystemFontOfSize:sizeFont] constrainedToSize:CGSizeMake(MAXFLOAT, 14) lineBreakMode:NSLineBreakByWordWrapping];        return size.width;    }else {        CGRect rect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 14) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:sizeFont]} context:nil];        return rect.size.width + 10;    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表