Skip to content

Commit

Permalink
Add new property useFontLineHeight to disable the auto-adjustment (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanpe committed Aug 19, 2021
1 parent dfc2d60 commit 5ce17f7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public extension UILabel {
set { multilineSpacing = newValue }
}

@IBInspectable
var useFontLineHeight: Bool {
get { usesTextHeightForLines }
set { usesTextHeightForLines = newValue }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public extension UITextView {
set { multilineSpacing = newValue }
}

@IBInspectable
var useFontLineHeight: Bool {
get { usesTextHeightForLines }
set { usesTextHeightForLines = newValue }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5ce17f7

Please sign in to comment.