IOS8.4下用- (CGRect)boundingRectWithSize: options: attributes: context:计算文本高度会崩溃的问题

这几天遇到一个计算文本高度的坑,这里记录一下。

我最开始在计算一段文本的高度的时候用的下面的代码:

CGSize msgWH = [_MsgModel.msg boundingRectWithSize:CGSizeMake(WBScreenW - msgX, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size:13]} context:nil].size;

然后就出现了下面这个问题

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                  0x18546c2d8 __exceptionPreprocess + 132
1   libobjc.A.dylib                 0x1971380e4 objc_exception_throw + 60
2   CoreFoundation                  0x1853589c8 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 420
3   CoreFoundation                  0x1853587f8 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 72

后面就参考这篇帖子
http://stackoverflow.com/questions/13621084/boundingrectwithsize-for-nsattributedstring-returning-wrong-size
改成下面这样:

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"PingFang SC" size:13], NSFontAttributeName,nil, NSForegroundColorAttributeName,nil];
NSMutableAttributedString *string =[[NSMutableAttributedString alloc] initWithString: _MsgModel.msg attributes:attributesDictionary];
CGSize msgWH = [string boundingRectWithSize:CGSizeMake(WBScreenW - msgX - WBSpacing, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil].size;

然后程序就不会crash了,但是在IOS9以下的版本会出现字体显示问题,字体变的很小,而且会影响到别的栏位,在IOS9以上的版本则一切正常,后面发现是[UIFont fontWithName:@”PingFang SC” size:13]这个字体的原因,PingFang SC这个字体是IOS9.0.2以后才有的,所以在IOS8上才会有这个问题,这也是为什么最开始的那种写法会导致程序崩溃的原因,因为在IOS8的系统下,@{NSFontAttributeName:[UIFont fontWithName:@”PingFang SC” size:13]}会导致crash,可以参考http://www.tuicool.com/articles/Y3EfAv7,然后就把字体改为使用系统字体就正常了。

后面又发现个别手机在IOS9.3下又crash了:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Filtered syslog:
None found

Last Exception Backtrace:
0   CoreFoundation                  0x18357adb0 __exceptionPreprocess + 124
1   libobjc.A.dylib                 0x182bdff80 objc_exception_throw + 56
2   CoreFoundation                  0x18357acf8 +[NSException raise:format:] + 120
3   Foundation                      0x183e698f0 -[NSConcreteMutableAttributedString initWithString:attributes:] + 136

log说是NSMutableAttributedString *string =[[NSMutableAttributedString alloc] initWithString: _MsgModel.msg attributes:attributesDictionary];的问题,然后我又换了种写法

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName,nil, NSForegroundColorAttributeName,nil];

CGSize msgWH = [_MsgModel.msg boundingRectWithSize:CGSizeMake(WBScreenW - msgX, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil].size;

你可能感兴趣的:(IOS开发)