Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions LabelSwitch/Classes/LabelSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ private class LabelSwitchPart {
return label
}()

let image: UIImageView = {
let img = UIImageView(frame: CGRect(x: 2, y: 2, width: 13, height: 13))
img.contentMode = .scaleAspectFit
return img
}()

let back: LabelSwitchBackView = {
let view = LabelSwitchBackView()
return view
Expand All @@ -39,6 +45,7 @@ private class LabelSwitchPart {
label.textColor = config.textColor
label.text = config.text
label.font = config.font
image.image = config.image
label.sizeToFit()

if let gradient = config.backGradient {
Expand All @@ -52,6 +59,7 @@ private class LabelSwitchPart {
back.imageView.image = image
back.imageView.isHidden = false
}

}

func setState(_ state: LabelSwitchPartState) {
Expand All @@ -76,13 +84,15 @@ private class LabelSwitchPart {
let part = LabelSwitchPart()
addSubview(part.back)
addSubview(part.label)
addSubview(part.image)
return part
}()

private lazy var rightPart: LabelSwitchPart = {
let part = LabelSwitchPart()
addSubview(part.back)
addSubview(part.label)
addSubview(part.image)
return part
}()

Expand Down Expand Up @@ -148,6 +158,7 @@ private class LabelSwitchPart {
layer.cornerRadius = calculatedSize.height / 2
setupTextMask()
setupLabel()
setupImage()
setupCircle()
}
}
Expand Down Expand Up @@ -190,9 +201,13 @@ private class LabelSwitchPart {
let circleMinimumSize = minimumSize.height - 2 * circlePadding
let circleSize = max(circleMinimumSize, max(switchConfigL.font.pointSize, switchConfigR.font.pointSize) * 2)
edge = circleSize * 0.2

let width = max(minimumSize.width, max(leftPart.label.bounds.width, rightPart.label.bounds.width) + 2 * edge + circleSize + 2 * circlePadding)
calculatedSize = CGSize(width: width, height: circleSize + 2 * circlePadding)
if (leftPart.label.text == "") || (rightPart.label.text == "") {
let width = max(minimumSize.width, max(leftPart.image.bounds.width, rightPart.image.bounds.width) + 2 * edge + circleSize + 2 * circlePadding)
calculatedSize = CGSize(width: width, height: circleSize + 2 * circlePadding)
} else {
let width = max(minimumSize.width, max(leftPart.label.bounds.width, rightPart.label.bounds.width) + 2 * edge + circleSize + 2 * circlePadding)
calculatedSize = CGSize(width: width, height: circleSize + 2 * circlePadding)
}
}

/// Calculate the left frame and right frame for the circle
Expand Down Expand Up @@ -220,6 +235,14 @@ private class LabelSwitchPart {
y: bounds.height / 2)
}

private func setupImage(){
leftPart.image.center = CGPoint(x: (bounds.width - bounds.height + edge) / 2,
y: bounds.height / 2)

rightPart.image.center = CGPoint(x: (bounds.width + bounds.height - edge) / 2,
y: bounds.height / 2)
}

/// Set the frame for the text mask
private func setupTextMask() {
stateL.leftPartState.backMaskFrame = bounds.offsetBy(dx: -bounds.width, dy: 0)
Expand Down
6 changes: 6 additions & 0 deletions LabelSwitch/Classes/LabelSwitchConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public struct LabelSwitchConfig {
var backgroundColor: UIColor
var backGradient: GradientBack?
var backImage: UIImage?
var image: UIImage?

public init(text: String, textColor: UIColor, font: UIFont, backgroundColor: UIColor) {
self.text = text
Expand All @@ -41,6 +42,11 @@ public struct LabelSwitchConfig {
self.backImage = image
}

public init(text: String, textColor: UIColor, font: UIFont, labelImage: UIImage?, background: UIColor) {
self.init(text: text, textColor: textColor, font: font, backgroundColor: background)
self.image = labelImage
}

public static let defaultLeft = LabelSwitchConfig(text: "Left",
textColor: .white,
font: .boldSystemFont(ofSize: 20),
Expand Down