From 9e61ff3a85a3780a2cdfceae0257ad32fa8ae4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanpe=20Catal=C3=A1n?= Date: Tue, 18 Jan 2022 15:20:32 +0100 Subject: [PATCH] Fix crash when text is empty (#483) --- .../SkeletonExtensions/SkeletonTextNode.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift b/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift index dad8f0e8..84233486 100644 --- a/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift +++ b/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift @@ -106,9 +106,11 @@ extension UILabel: SkeletonTextNode { } var fontLineHeight: CGFloat? { - if let attributes = attributedText?.attributes(at: 0, effectiveRange: nil), - let fontAttribute = attributes.first(where: { $0.key == .font }) { - return fontAttribute.value as? CGFloat ?? font.lineHeight + if let attributedText = attributedText, + attributedText.length > 0 { + let attributes = attributedText.attributes(at: 0, effectiveRange: nil) + let fontAttribute = attributes.first(where: { $0.key == .font }) + return fontAttribute?.value as? CGFloat ?? font.lineHeight } else { return font.lineHeight } @@ -179,9 +181,11 @@ extension UITextView: SkeletonTextNode { } var fontLineHeight: CGFloat? { - if let attributes = attributedText?.attributes(at: 0, effectiveRange: nil), - let fontAttribute = attributes.first(where: { $0.key == .font }) { - return fontAttribute.value as? CGFloat ?? font?.lineHeight + if let attributedText = attributedText, + attributedText.length > 0 { + let attributes = attributedText.attributes(at: 0, effectiveRange: nil) + let fontAttribute = attributes.first(where: { $0.key == .font }) + return fontAttribute?.value as? CGFloat ?? font?.lineHeight } else { return font?.lineHeight }