Skip to content

Commit

Permalink
feat: added filling percent to last line in elements with multilines
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanpe committed Nov 22, 2017
1 parent 9523446 commit 4e63a2a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="isSkeletonable" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="lastLineFillPercent">
<integer key="value" value="40"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</textView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="avatar" translatesAutoresizingMaskIntoConstraints="NO" id="nMj-pU-5wJ">
Expand Down Expand Up @@ -204,7 +207,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-594.39999999999998" y="85.007496251874073"/>
<point key="canvasLocation" x="-482" y="-6"/>
</scene>
</scenes>
<resources>
Expand Down
10 changes: 6 additions & 4 deletions Sources/Extensions/CALayer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ extension CALayer {
return sublayers?.filter { $0.name == CALayer.skeletonSubLayersName } ?? [CALayer]()
}

func addMultilinesLayers(lines: Int, type: SkeletonType) {
func addMultilinesLayers(lines: Int, type: SkeletonType, lastLineFillPercent: Int) {
let numberOfSublayers = calculateNumLines(maxLines: lines)
for index in 0..<numberOfSublayers {
var width = Int(bounds.width)
if SkeletonDefaultConfig.multilineLastLineShorter && index == numberOfSublayers-1 && numberOfSublayers != 1 {
width -= Int(Double(width)*0.2);
var width = bounds.width

if index == numberOfSublayers-1 && numberOfSublayers != 1 {
width = width * CGFloat(lastLineFillPercent)/100;
}

let layer = SkeletonLayerFactory().makeMultilineLayer(withType: type, for: index, width: width)
addSublayer(layer)
}
Expand Down
36 changes: 35 additions & 1 deletion Sources/Helpers/ContainsMultilineText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,51 @@

import UIKit

private enum AssociatedKeys {
static var lastLineFillingPercent = "lastLineFillingPercent"
}

protocol ContainsMultilineText {
var numLines: Int { get }
var lastLineFillingPercent: Int { get }
}

extension ContainsMultilineText {
var numLines: Int { return 0 }
}

public extension UILabel {

@IBInspectable
var lastLineFillPercent: Int {
get { return lastLineFillingPercent }
set { lastLineFillingPercent = min(newValue, 100) }
}
}

public extension UITextView {

@IBInspectable
var lastLineFillPercent: Int {
get { return lastLineFillingPercent }
set { lastLineFillingPercent = min(newValue, 100) }
}
}

extension UILabel: ContainsMultilineText {
var numLines: Int {
return numberOfLines
}

var lastLineFillingPercent: Int {
get { return objc_getAssociatedObject(self, &AssociatedKeys.lastLineFillingPercent) as? Int ?? SkeletonDefaultConfig.multilineLastLineFillPercent }
set { objc_setAssociatedObject(self, &AssociatedKeys.lastLineFillingPercent, newValue, AssociationPolicy.retain.objc) }
}
}
extension UITextView: ContainsMultilineText {

var lastLineFillingPercent: Int {
get { return objc_getAssociatedObject(self, &AssociatedKeys.lastLineFillingPercent) as? Int ?? SkeletonDefaultConfig.multilineLastLineFillPercent }
set { objc_setAssociatedObject(self, &AssociatedKeys.lastLineFillingPercent, newValue, AssociationPolicy.retain.objc) }
}
}
extension UITextView: ContainsMultilineText {}
6 changes: 3 additions & 3 deletions Sources/SkeletonDefaultConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public enum SkeletonDefaultConfig {

public static let gradient = SkeletonGradient(baseColor: tintColor)

public static let multilineHeight = 15
public static let multilineHeight: CGFloat = 15

public static let multilineSpacing = 10
public static let multilineSpacing: CGFloat = 10

public static let multilineLastLineShorter = true
public static let multilineLastLineFillPercent = 70
}
8 changes: 4 additions & 4 deletions Sources/SkeletonLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class SkeletonLayerFactory {
return SkeletonLayer(withType: type, usingColors: colors, andSkeletonHolder: holder)
}

func makeMultilineLayer(withType type: SkeletonType, for index: Int, width: Int) -> CALayer {
let spaceRequitedForEachLine = SkeletonDefaultConfig.multilineHeight + SkeletonDefaultConfig.multilineSpacing
func makeMultilineLayer(withType type: SkeletonType, for index: Int, width: CGFloat) -> CALayer {
let spaceRequiredForEachLine = SkeletonDefaultConfig.multilineHeight + SkeletonDefaultConfig.multilineSpacing
let layer = type.layer
layer.anchorPoint = .zero
layer.name = CALayer.skeletonSubLayersName
layer.frame = CGRect(x: 0, y: (index * spaceRequitedForEachLine), width: width, height: SkeletonDefaultConfig.multilineHeight)
layer.frame = CGRect(x: 0.0, y: CGFloat(index) * spaceRequiredForEachLine, width: width, height: SkeletonDefaultConfig.multilineHeight)
return layer
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ struct SkeletonLayer {

func addMultilinesIfNeeded() {
guard let multiLineView = holder as? ContainsMultilineText else { return }
maskLayer.addMultilinesLayers(lines: multiLineView.numLines, type: type)
maskLayer.addMultilinesLayers(lines: multiLineView.numLines, type: type, lastLineFillPercent: multiLineView.lastLineFillingPercent)
}
}

Expand Down

0 comments on commit 4e63a2a

Please sign in to comment.