获取字符串宽度

//获取字符串的宽度

-(CGSize)getHWKnowledgeWidthfontSize:(float)fontSize andHeight:(float)height
{
    if ([self isEqualToString:@""]) {
        return CGSizeZero;
    }
    CGRect stringRect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
    CGSize size = CGSizeMake(ceil(stringRect.size.width), ceil(stringRect.size.height));
    return size;
    
}

你可能感兴趣的:(获取字符串宽度)