From b9bfac1652c75afc3cd0479665b032769ca00db5 Mon Sep 17 00:00:00 2001 From: mohammadsuhail98 Date: Mon, 26 Oct 2020 09:52:20 +0300 Subject: [PATCH] Ability to add icons instead of labels. --- LabelSwitch/Classes/LabelSwitch.swift | 29 ++++++++++++++++++--- LabelSwitch/Classes/LabelSwitchConfig.swift | 6 +++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/LabelSwitch/Classes/LabelSwitch.swift b/LabelSwitch/Classes/LabelSwitch.swift index 3502742..d709a30 100644 --- a/LabelSwitch/Classes/LabelSwitch.swift +++ b/LabelSwitch/Classes/LabelSwitch.swift @@ -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 @@ -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 { @@ -52,6 +59,7 @@ private class LabelSwitchPart { back.imageView.image = image back.imageView.isHidden = false } + } func setState(_ state: LabelSwitchPartState) { @@ -76,6 +84,7 @@ private class LabelSwitchPart { let part = LabelSwitchPart() addSubview(part.back) addSubview(part.label) + addSubview(part.image) return part }() @@ -83,6 +92,7 @@ private class LabelSwitchPart { let part = LabelSwitchPart() addSubview(part.back) addSubview(part.label) + addSubview(part.image) return part }() @@ -148,6 +158,7 @@ private class LabelSwitchPart { layer.cornerRadius = calculatedSize.height / 2 setupTextMask() setupLabel() + setupImage() setupCircle() } } @@ -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 @@ -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) diff --git a/LabelSwitch/Classes/LabelSwitchConfig.swift b/LabelSwitch/Classes/LabelSwitchConfig.swift index 48ab204..1f16897 100644 --- a/LabelSwitch/Classes/LabelSwitchConfig.swift +++ b/LabelSwitch/Classes/LabelSwitchConfig.swift @@ -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 @@ -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),