diff --git a/README.md b/README.md index 7d6f275e..07287296 100755 --- a/README.md +++ b/README.md @@ -518,6 +518,14 @@ By default, the user interaction is disabled for skeletonized items, but if you view.isUserInteractionDisabledWhenSkeletonIsActive = false // The view will be active when the skeleton will be active. ``` +**Don't use the font line height for the skeleton lines in labels** + +False to disable skeleton to auto-adjust to font height for a `UILabel` or `UITextView`. By default, the skeleton lines height is auto-adjusted to font height to more accurately reflect the text in the label rect rather than using the bounding box. + +```swift +label.useFontLineHeight = false +``` + **Delayed show skeleton** You can delay the presentation of the skeleton if the views update quickly. diff --git a/SkeletonViewCore/Sources/API/UIKitExtensions/UILabel+IBInspectable.swift b/SkeletonViewCore/Sources/API/UIKitExtensions/UILabel+IBInspectable.swift index 32c42ffa..00138607 100644 --- a/SkeletonViewCore/Sources/API/UIKitExtensions/UILabel+IBInspectable.swift +++ b/SkeletonViewCore/Sources/API/UIKitExtensions/UILabel+IBInspectable.swift @@ -33,4 +33,10 @@ public extension UILabel { set { multilineSpacing = newValue } } + @IBInspectable + var useFontLineHeight: Bool { + get { usesTextHeightForLines } + set { usesTextHeightForLines = newValue } + } + } diff --git a/SkeletonViewCore/Sources/API/UIKitExtensions/UITextView+IBInspectable.swift b/SkeletonViewCore/Sources/API/UIKitExtensions/UITextView+IBInspectable.swift index 316ccfd6..52d39c73 100644 --- a/SkeletonViewCore/Sources/API/UIKitExtensions/UITextView+IBInspectable.swift +++ b/SkeletonViewCore/Sources/API/UIKitExtensions/UITextView+IBInspectable.swift @@ -33,4 +33,10 @@ public extension UITextView { set { multilineSpacing = newValue } } + @IBInspectable + var useFontLineHeight: Bool { + get { usesTextHeightForLines } + set { usesTextHeightForLines = newValue } + } + } diff --git a/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift b/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift index 49bb04bc..74696bd0 100644 --- a/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift +++ b/SkeletonViewCore/Sources/Internal/SkeletonExtensions/SkeletonTextNode.swift @@ -22,27 +22,36 @@ protocol SkeletonTextNode { var multilineCornerRadius: Int { get } var multilineSpacing: CGFloat { get } var paddingInsets: UIEdgeInsets { get } - + var usesTextHeightForLines: Bool { get } } enum SkeletonTextNodeAssociatedKeys { + static var lastLineFillingPercent = "lastLineFillingPercent" static var multilineCornerRadius = "multilineCornerRadius" static var multilineSpacing = "multilineSpacing" static var paddingInsets = "paddingInsets" static var backupHeightConstraints = "backupHeightConstraints" + static var usesTextHeightForLines = "usesTextHeightForLines" + } extension UILabel: SkeletonTextNode { var lineHeight: CGFloat { - if let fontLineHeight = font?.lineHeight { - if let heightConstraints = backupHeightConstraints.first?.constant { - return (fontLineHeight > heightConstraints) ? heightConstraints : fontLineHeight - } - return fontLineHeight + let constraintsLineHeight = backupHeightConstraints.first?.constant ?? SkeletonAppearance.default.multilineHeight + + if useFontLineHeight, + let fontLineHeight = font?.lineHeight { + return fontLineHeight > constraintsLineHeight ? constraintsLineHeight : fontLineHeight + } else { + return constraintsLineHeight } - return SkeletonAppearance.default.multilineHeight + } + + var usesTextHeightForLines: Bool { + get { return ao_get(pkey: &SkeletonTextNodeAssociatedKeys.usesTextHeightForLines) as? Bool ?? true } + set { ao_set(newValue, pkey: &SkeletonTextNodeAssociatedKeys.usesTextHeightForLines) } } var lastLineFillingPercent: Int { @@ -75,15 +84,19 @@ extension UILabel: SkeletonTextNode { extension UITextView: SkeletonTextNode { var lineHeight: CGFloat { - if let fontLineHeight = font?.lineHeight { - if let heightConstraints = heightConstraints.first?.constant { - return (fontLineHeight > heightConstraints) ? heightConstraints : fontLineHeight - } - - return fontLineHeight - } + let constraintsLineHeight = heightConstraints.first?.constant ?? SkeletonAppearance.default.multilineHeight - return SkeletonAppearance.default.multilineHeight + if useFontLineHeight, + let fontLineHeight = font?.lineHeight { + return fontLineHeight > constraintsLineHeight ? constraintsLineHeight : fontLineHeight + } else { + return constraintsLineHeight + } + } + + var usesTextHeightForLines: Bool { + get { return ao_get(pkey: &SkeletonTextNodeAssociatedKeys.usesTextHeightForLines) as? Bool ?? true } + set { ao_set(newValue, pkey: &SkeletonTextNodeAssociatedKeys.usesTextHeightForLines) } } var numberOfLines: Int {