YYKit源码探究(四) —— NSString分类之Drawing(三)

版本记录

版本号 时间
V1.0 2018.03.20

前言

iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - ibireme等。这里有一篇唐巧对他的专访,还有他的 GitHub - Yaoyuan博客,这里贴出来框架YYKit 框架。接下来几篇我们就一起来看一下这个框架。感兴趣的可以看上面写的几篇。
1. YYKit源码探究(一) —— 基本概览
2. YYKit源码探究(二) —— NSString分类之Hash(一)
3. YYKit源码探究(三) —— NSString分类之Encode and decode(二)

回顾

上一篇我们分析了NSString分类NSString+YYAddEncode and decode部分,这一篇我们就看一下Drawing部分。


API 接口

/**
 Returns the size of the string if it were rendered with the specified constraints.
 
 @param font          The font to use for computing the string size.
 
 @param size          The maximum acceptable size for the string. This value is
 used to calculate where line breaks and wrapping would occur.
 
 @param lineBreakMode The line break options for computing the size of the string.
 For a list of possible values, see NSLineBreakMode.
 
 @return              The width and height of the resulting string's bounding box.
 These values may be rounded up to the nearest whole number.
 */
- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode;

/**
 Returns the width of the string if it were to be rendered with the specified
 font on a single line.
 
 @param font  The font to use for computing the string width.
 
 @return      The width of the resulting string's bounding box. These values may be
 rounded up to the nearest whole number.
 */
- (CGFloat)widthForFont:(UIFont *)font;

/**
 Returns the height of the string if it were rendered with the specified constraints.
 
 @param font   The font to use for computing the string size.
 
 @param width  The maximum acceptable width for the string. This value is used
 to calculate where line breaks and wrapping would occur.
 
 @return       The height of the resulting string's bounding box. These values
 may be rounded up to the nearest whole number.
 */
- (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width;

1. - (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode;

这个方法的作用就是返回给定的字符串边界的宽度和高度。这里size参数的意思是字符串的最大可接受大小。 此值用于计算行会发生断裂和换行的位置。

示例调用

下面我们看一下方法的示例调用。

NSString *str = @"主卧室胡搜花生豆我就搜好的生动和我胡搜红色吼吼吼2个为WIFI个我哦狗狗狗会NOI哈哈那是嘉实基金。";
CGSize stringSize = [str sizeForFont:[UIFont systemFontOfSize:16.0] size:CGSizeMake(200, 500) mode:NSLineBreakByWordWrapping];
NSLog(@"字符串大小为 = %@", NSStringFromCGSize(stringSize));

下面看一下输出结果

2018-03-19 09:57:22.261885+0800 JJWebImage[29086:4093284] 字符串大小为 = {195.83999999999997, 76.375}

方法实现

下面我们就看一下方法实现

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

这里其实还是利用NSString原生的方法boundingRectWithSize:options:attributes:context:

2. - (CGFloat)widthForFont:(UIFont *)font;

该方法的作用就是返回一行指定字体的字符串的宽度。

示例调用

下面我们就看一下示例调用。

NSString *str = @"主卧室胡搜花生豆我就搜好的生动和我胡搜红色吼吼吼2个为WIFI个我哦狗狗狗会NOI哈哈那是嘉实基金。";
CGFloat fontWidth = [str widthForFont:[UIFont systemFontOfSize:20.0]];
NSLog(@"字符串宽度为 = %lf", fontWidth);

下面看一下实现结果

2018-03-19 10:12:01.718661+0800 JJWebImage[29090:4096887] 字符串宽度为 = 941.292031

方法实现

下面我们就看一下方法实现

- (CGFloat)widthForFont:(UIFont *)font {
    CGSize size = [self sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}
- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

这里默认传入到第二个方法中的size参数为

CGSizeMake(HUGE, HUGE)

#define HUGE        MAXFLOAT

传入的换行模式为NSLineBreakByWordWrapping

3. - (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width;

该方法的作用就是返回字符串的高度,这里width的作用就是用来计算换行发生的地方。

示例调用

下面我们就看一下示例调用。

NSString *str = @"主卧室胡搜花生豆我就搜好的生动和我胡搜红色吼吼吼2个为WIFI个我哦狗狗狗会NOI哈哈那是嘉实基金。";
CGFloat fontHeight = [str heightForFont:[UIFont systemFontOfSize:20.0] width:100];
NSLog(@"字符串高度为 = %lf", fontHeight);

下面看一下输出结果

2018-03-19 10:22:44.503334+0800 JJWebImage[29095:4099678] 字符串高度为 = 286.406250

方法实现

下面我们就看一下方法实现

- (CGFloat)widthForFont:(UIFont *)font {
    CGSize size = [self sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}

同样,这里也是调用上面那个方法,size还是默认为CGSizeMake(HUGE, HUGE),mode默认为NSLineBreakByWordWrapping

后记

本篇我们分析了Drawing部分的功能,主要就是获取指定字符串的size、width和height,喜欢的给个赞~~~

推荐阅读更多精彩内容