使用NSString方法boundingRectWithSize时在iOS 11上崩溃

时间:2018-01-22 09:35:27

标签: crash ios11

使用以下代码时在iOS 11上崩溃

NSString * str = @"                        しょーたฅ(๑•̀ω•́๑)ฅニャンニャンガオー★";
CGSize strSize = [str boundingRectWithSize:CGSizeMake(281.33333, 100) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;

您需要在XCode中设置一个异常断点,然后会出现以下堆栈。

    Thread 1 Queue : com.apple.main-thread (serial)
#0  0x00000001827384f0 in objc_exception_throw ()
#1  0x00000001834f22ac in +[NSException raise:format:] ()
#2  0x0000000183dfb560 in -[NSString rangeOfCharacterFromSet:options:range:] ()
#3  0x000000018dbd236c in -[NSParagraphArbitrator lineBreakContextBeforeIndex:lineFragmentWidth:range:] ()
#4  0x000000018dbbb864 in -[NSATSLineFragment _charIndexToBreakLineByWordWrappingAtIndex:lineFragmentWidth:hyphenate:] ()
#5  0x000000018dbbc67c in -[NSATSLineFragment layoutForStartingGlyphAtIndex:characterIndex:minPosition:maxPosition:lineFragmentRect:] ()
#6  0x000000018dbb390c in -[NSATSTypesetter _layoutLineFragmentStartingWithGlyphAtIndex:characterIndex:atPoint:renderingContext:] ()
#7  0x000000018dbb5028 in -[NSATSTypesetter layoutParagraphAtPoint:] ()
#8  0x000000018dc0e074 in -[NSTypesetter _layoutGlyphsInLayoutManager:startingAtGlyphIndex:maxNumberOfLineFragments:maxCharacterIndex:nextGlyphIndex:nextCharacterIndex:] ()
#9  0x000000018dc0f3e0 in -[NSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments:] ()
#10 0x000000018dbb5b68 in -[NSATSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments:] ()
#11 0x000000018dba13b8 in -[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] ()
#12 0x000000018dba2da8 in -[NSLayoutManager(NSPrivate) _fillLayoutHoleAtIndex:desiredNumberOfLines:] ()
#13 0x000000018dbaf9a0 in _NSFastFillAllLayoutHolesUpToEndOfContainerForGlyphIndex ()
#14 0x000000018dbd8c90 in -[NSLayoutManager ensureLayoutForTextContainer:] ()
#15 0x000000018dbf7908 in -[NSStringDrawingTextStorage usedRectForTextContainer:] ()
#16 0x000000018dbf0864 in __NSStringDrawingEngine ()
#17 0x000000018dbee8b4 in -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] ()
#18 0x00000001028b6664 in main at /Users/test/workspace/TestiOS11String/TestiOS11String/main.m:15
#19 0x0000000182ed456c in start ()

每当呈现行数设置为0的多行UILabel时,这会导致崩溃。

如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:0)

我还遇到了日语设置导致iOS 11崩溃的情况。

这是我的解决方案。添加了lineBreakMode = .byCharWrapping

let style = NSMutableParagraphStyle()
style.lineBreakMode = .byCharWrapping
let string: String = "                        しょーたฅ(๑•̀ω•́๑)ฅニャンニャンガ"
let size = string.boundingRect(
    with: CGSize(width: 281, height: CGFloat.greatestFiniteMagnitude),
    options: [.usesLineFragmentOrigin, .usesFontLeading, .usesDeviceMetrics],
    attributes: [.font: UIFont.systemFont(ofSize: 16),
                 .paragraphStyle: style],
    context: nil)

有趣的是,此文本在未设置.byCharWrapping的情况下也会在UILabel上崩溃。 (iOS 11,日语设置)

label.text = "                        しょーたฅ(๑•̀ω•́๑)ฅニャンニャンガ"
label.font = UIFont.systemFont(ofSize: 16)
//label.lineBreakMode = .byCharWrapping
label.frame = CGRect(x: 0, y: 0, width: 281, height: 500)
label.numberOfLines = 0
view.addSubview(label)