美文网首页iOS
iOS 计算NSString宽高与计算NSAttributedS

iOS 计算NSString宽高与计算NSAttributedS

作者: 1ace156a39cd | 来源:发表于2017-04-19 17:22 被阅读1294次

广告

欢迎大家一起交流 QQ群 139852091 公众号

我是jpg

开篇

项目有一个客服反馈功能,用到的是聊天列表的形式,这就免不了计算字符串的宽高,由于要给字符串加间距,没办法,只能用 NSAttributedString 所以要计算NSAttributedString的长宽

计算NSString宽高

计算NSString宽高很简单,代码如下:

//返回字符串所占用的尺寸.
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

参数解读

font 是当前字符串的字体(包含大小信息)
maxSize 是一个最大的距离:如我最大的宽度只让他为200,高度不限,则传入:

CGSizeMake(200 , CGFLOAT_MAX)

计算NSAttributedString宽高

先查看系统API:

Paste_Image.png

代码如下:

- (CGRect)boundsWithFontSize:(CGFloat)fontSize text:(NSString *)text needWidth:(CGFloat)needWidth lineSpacing:(CGFloat )lineSpacing
{
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    
    style.lineSpacing = lineSpacing;
    
    UIFont *font = Font_Light(fontSize);
    
    [attributeString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
    
    [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];
    
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    CGRect rect = [attributeString boundingRectWithSize:CGSizeMake(needWidth, CGFLOAT_MAX) options:options context:nil];
    
    
    
    return rect;
    
}

参数解读

fontSize :是当前字符串的字体(包含大小信息)
text:将要计算的�字符串
needWidth:将要计算的最大宽度
lineSpacing:行间距大小

当然关于 NSAttributedString 的设置还有很多,不一一列举了,提供一个同事封装好的留下以后用

调整行间距
//调整行间距
+ (NSMutableAttributedString *)atttibutedStringForString:(NSString *)string LineSpace:(CGFloat)lineSpace {

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

    return attributedString;
  
}
设置属性文字
+ (NSMutableAttributedString *)attributedStringForString:(NSString *)string attributeds:(NSDictionary *)attributeds hasPrefix:(NSString *)hasPrefix  hasSuffix:(NSString *)hasSuffix {


    if (string.length == 0) {

        return nil;
    }

    if (hasSuffix.length ==0) {

        hasSuffix = @"";
    }

    if (hasPrefix.length == 0) {
        hasPrefix = @"";

    }

    NSString *str = [NSString stringWithFormat:@"%@%@%@",hasPrefix,string,hasSuffix];

    NSRange range = [str rangeOfString:string];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:str];

    [attributedString addAttributes:attributeds range:range];


    if ([attributeds objectForKey:@"NSParagraphStyle"]) {

        NSMutableParagraphStyle * paragraphStyle = [attributeds objectForKey:@"NSParagraphStyle"];

        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

    }

    
    return attributedString;
    
}

广告

欢迎大家一起交流 QQ群 139852091 公众号

我是jpg

相关文章

网友评论

  • PGOne爱吃饺子:字符串加间距是什么? 根据字符串计算宽高应该很容易啊,楼主为什么写的这么麻烦??
    - (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
    {
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    }
    这一句话不就行了么
    1ace156a39cd:@4140d18ee6fc 富文本添加字间距的时候

本文标题:iOS 计算NSString宽高与计算NSAttributedS

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