Juanpe / SkeletonView

โ˜ ๏ธ An elegant way to show users that something is happening and also prepare them to which contents they are awaiting
MIT License
12.51k stars 1.1k forks source link

Crash in version 1.26 with textLineHeight = .relativeToFont #482

Closed dtamiazzo closed 2 years ago

dtamiazzo commented 2 years ago

Description

Hi all, we're experiencing a crash with the new version of the library (1.26.0) with the SkeletonAppearance with textLineHeight = .relativeToFont. The app crashes with "NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds" when calling layoutSkeletonIfNeeded if the UILabel to Skeletonize has nil text or is empty.

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])


Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

SkeletonView Environment:

SkeletonView version: 1.26.0 Xcode version: 13.2.1 **Swift version: Swift 5.5.2

Steps to reproduce:

Please replace this with the steps to reproduce the behavior.

  1. create a label = UILabel() with label.text = nil and set label.isSkeletonable = true
  2. set SkeletonAppearance.default.textLineHeight = .relativeToFont
  3. call showSkeleton() on the label
  4. the app crashes on the line 94 of SkeletonTextNode.swift: if let attributes = attributedText?.attributes(at: 0, effectiveRange: nil)

Expected result:

The app shouldn't crash.

Actual result:

The app crashes with "NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds"

Attachments:

I suggest this edit at line 93 of SkeletonTextNode.swift:

var fontLineHeight: CGFloat? {
    if let attributedText = attributedText,
       attributedText.string.count > 0,
       let fontAttribute = attributedText.attributes(at: 0, effectiveRange: nil).first(where: { $0.key == .font }) {
        return fontAttribute.value as? CGFloat ?? font.lineHeight
    } else {
        return font.lineHeight
    }
}
Juanpe commented 2 years ago

Hi @dtamiazzo ๐Ÿ‘‹๐Ÿผ Thanks for reporting this issue.

Good catch! I'll fix it asap ๐Ÿ‘๐Ÿผ

Juanpe commented 2 years ago

This fix was fixed in the version 1.29.2. Thanks for reporting ๐Ÿ‘๐Ÿผ