UILabel Crash with Attributed Text

In Xcode version 10.0 and swift 4.2 my app crashes when setting the content of UILabel.attributedText and accessing any of UILabels variables.


For example

let label = UILabel()

label.attrubitedText = NSAttirbutedText("String")

label.sizeToFit() //this will crash the App

label. (any method or variable) will crash the app


If you don't set the attributed text variable then any follow on calls to label work fine.


What needs to be done to stop the crash?


Help

Pls. share any errors related to your crash, thanks.




___________

Tags:For Best Results - Read the Label

Did you copy the exact code ?


Surely not, because there are a lot of typos.


I tested this, it works


        let label = UILabel()
        label.attributedText = NSAttributedString(string: "String")
        label.sizeToFit()   //this will crash the App
        print(label)


So please, when you ask :

- copy the exact code, do not retype with errors

- tell what exact message you get

Also tested a property


        print(label.isEnabled)


No crash, I get true in log.

Here is a sample of the code that causes the crash.


It crashes at line 14 with an unrecognized selector sent to instance. Line 13 executes successfully.

If I don't set the attributedText then it works fine.


private func centeredAttributedString(_ string: String, fontSize: CGFloat) -> NSAttributedString {
    var font = UIFont.preferredFont(forTextStyle: .body).withSize(fontSize)
    font = UIFontMetrics(forTextStyle: .body).scaledFont(for: font) // allows user to change scale of font
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    return NSAttributedString(string: string, attributes: [.paragraphStyle:paragraphStyle, .font:fontSize])
    
}

let label = UILabel()
label.frame.size = CGSize.zero
label.attributedText = centeredAttributedString("Attributed String", fontSize: 35.0)
print(label)
label.sizeToFit()
label.isHidden = false

The error is :

    return NSAttributedString(string: string, attributes: [.paragraphStyle:paragraphStyle, .font:fontSize])


font parameter is a font, not a fontSize.

Change with:

        return NSAttributedString(string: string, attributes: [.paragraphStyle:paragraphStyle, .font: font])
As per Apple document:

Important
A paragraph style object should not be mutated after adding it to an attributed string; doing so can cause your app to crash.

please check it!

`1 CoreText TFont::UserFallbacksForLanguage(__CFString const*) const (in CoreText) + 184 2 CoreText TFontCascade::InitParams() (in CoreText) + 476 3 CoreText TFontCascade::InitParams() (in CoreText) + 476 4 CoreText TFontCascade::CreateFallback(__CTFont const*, __CFString const*, CTEmojiPolicy) const (in CoreText) + 88 5 CoreText TGlyphEncoder::AppendUnmappedCharRun(unsigned int, TCFRef<CTRun*>&, __CTFont const*, CFRange&, CFRange, TInlineVector<long, 30ul>&, TFontCascade const&, TGlyphEncoder::ClusterMatching, bool) (in CoreText) + 720 6 CoreText TGlyphEncoder::RunUnicodeEncoderRecursively(unsigned int, TCFRef<CTRun*>&&, __CTFont const*, CFRange, TInlineVector<long, 30ul>&, TGlyphEncoder::Fallbacks, TFontCascade const*, __CFString const*, TGlyphEncoder::ClusterMatching, bool) (in CoreText) + 1424 7 CoreText TGlyphEncoder::EncodeChars(CFRange, TAttributes const&, TGlyphEncoder::Fallbacks) (in CoreText) + 1412 8 CoreText TTypesetterAttrString::Initialize(__CFAttributedString const*) (in CoreText) + 448 9 CoreText TTypesetterAttrString::TTypesetterAttrString(__CFAttributedString const*, __CFDictionary const*) (in CoreText) + 160 10 CoreText CTLineCreateWithAttributedString (in CoreText) + 132 11 UIFoundation -[NSCoreTypesetter _stringDrawingCoreTextEngineWithOriginalString:rect:padding:graphicsContext:forceClipping:attributes:stringDrawingOptions:drawingContext:stringDrawingInterface:] (in UIFoundation) + 1740 12 UIFoundation __NSStringDrawingEngine (in UIFoundation) + 1652 13 UIFoundation -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] (in UIFoundation) + 156 14 UIKitCore -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] (in UIKitCore) + 1220 15 UIKitCore -[UILabel _intrinsicSizeWithinSize:] (in UIKitCore) + 484 16 UIKitCore -[UIView(Geometry) sizeToFit] (in UIKitCore) + 136

UILabel Crash with Attributed Text
 
 
Q