From 607ade1cb04df820d00c52f51b71eeea258ecad7 Mon Sep 17 00:00:00 2001 From: JinUng41 Date: Sun, 30 Jun 2024 13:52:40 +0900 Subject: [PATCH] =?UTF-8?q?add/#89=20Extension=20=EB=B0=8F=20Util=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extension/NSAttributedString+.swift | 26 ++++++++++ KkuMulKum/Resource/Extension/UIButton+.swift | 31 +++++++++++ KkuMulKum/Resource/Extension/UIFont+.swift | 45 ++++++++++++++++ KkuMulKum/Resource/Extension/UILabel+.swift | 37 +++++++++++++ .../Resource/Extension/UIStackView+.swift | 21 ++++++++ .../Resource/Extension/UITextField+.swift | 52 +++++++++++++++++++ KkuMulKum/Resource/Extension/UIView+.swift | 27 ++++++++++ .../Resource/Util/ReuseIdentifiable.swift | 14 +++++ KkuMulKum/Resource/Util/Screen.swift | 22 ++++++++ 9 files changed, 275 insertions(+) create mode 100644 KkuMulKum/Resource/Extension/NSAttributedString+.swift create mode 100644 KkuMulKum/Resource/Extension/UIButton+.swift create mode 100644 KkuMulKum/Resource/Extension/UIFont+.swift create mode 100644 KkuMulKum/Resource/Extension/UILabel+.swift create mode 100644 KkuMulKum/Resource/Extension/UIStackView+.swift create mode 100644 KkuMulKum/Resource/Extension/UITextField+.swift create mode 100644 KkuMulKum/Resource/Extension/UIView+.swift create mode 100644 KkuMulKum/Resource/Util/ReuseIdentifiable.swift create mode 100644 KkuMulKum/Resource/Util/Screen.swift diff --git a/KkuMulKum/Resource/Extension/NSAttributedString+.swift b/KkuMulKum/Resource/Extension/NSAttributedString+.swift new file mode 100644 index 00000000..725c422c --- /dev/null +++ b/KkuMulKum/Resource/Extension/NSAttributedString+.swift @@ -0,0 +1,26 @@ +// +// NSAttributedString+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension NSAttributedString { + static func pretendardString( + _ text: String = "", + style: UIFont.Pretendard + ) -> NSAttributedString { + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.paragraphSpacing = style.leading + + let attributes: [NSAttributedString.Key: Any] = [ + .paragraphStyle: paragraphStyle, + .font: UIFont.pretendard(style), + .kern: style.tracking + ] + + return NSAttributedString(string: text, attributes: attributes) + } +} diff --git a/KkuMulKum/Resource/Extension/UIButton+.swift b/KkuMulKum/Resource/Extension/UIButton+.swift new file mode 100644 index 00000000..2d2492fa --- /dev/null +++ b/KkuMulKum/Resource/Extension/UIButton+.swift @@ -0,0 +1,31 @@ +// +// UIButton+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension UIButton { + func setTitle(_ title: String, style: UIFont.Pretendard, color: UIColor) { + setAttributedTitle(.pretendardString(title, style: style), for: .normal) + setTitleColor(color, for: .normal) + } + + func setLayer(borderWidth: CGFloat = 0, borderColor: UIColor, cornerRadius: CGFloat) { + layer.borderColor = borderColor.cgColor + layer.cornerRadius = cornerRadius + layer.borderWidth = borderWidth + } + + func addUnderline() { + let attributedString = NSMutableAttributedString(string: self.titleLabel?.text ?? "") + attributedString.addAttribute( + .underlineStyle, + value: NSUnderlineStyle.single.rawValue, + range: NSRange(location: 0, length: attributedString.length) + ) + setAttributedTitle(attributedString, for: .normal) + } +} diff --git a/KkuMulKum/Resource/Extension/UIFont+.swift b/KkuMulKum/Resource/Extension/UIFont+.swift new file mode 100644 index 00000000..d26f4fd5 --- /dev/null +++ b/KkuMulKum/Resource/Extension/UIFont+.swift @@ -0,0 +1,45 @@ +// +// UIFont+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension UIFont { + static func pretendard(_ style: Pretendard) -> UIFont { + return UIFont(name: style.weight, size: style.size) ?? .systemFont(ofSize: style.size) + } + + enum Pretendard { + case title01, title02 + case head01, head02 + case body01, body02, body03, body04, body05, body06 + case caption01, caption02 + case label01, label02 + + var weight: String { + switch self { + case .title01, .head01, .body01, .body03, .body05, .caption01, .label01: "Pretendard-SemiBold" + case .title02, .head02, .body02, .body04, .body06, .caption02, .label02: "Pretendard-Regular" + } + } + + var size: CGFloat { + switch self { + case .title01, .title02: 24 + case .head01, .head02: 22 + case .body01, .body02: 18 + case .body03, .body04: 16 + case .body05, .body06: 14 + case .caption01, .caption02: 12 + case .label01, .label02: 10 + } + } + + var tracking: CGFloat { CGFloat(-2) / 100 * size } + + var leading: CGFloat { (1.6 - 1) * size } + } +} diff --git a/KkuMulKum/Resource/Extension/UILabel+.swift b/KkuMulKum/Resource/Extension/UILabel+.swift new file mode 100644 index 00000000..057b5bfb --- /dev/null +++ b/KkuMulKum/Resource/Extension/UILabel+.swift @@ -0,0 +1,37 @@ +// +// UILabel+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension UILabel { + func setText(_ text: String, style: UIFont.Pretendard, color: UIColor) { + attributedText = .pretendardString(text, style: style) + textColor = color + numberOfLines = 0 + } + + func setHighlightText(_ words: String..., style: UIFont.Pretendard, color: UIColor? = nil) { + guard let currentText = attributedText?.string else { return } + let mutableAttributedString = NSMutableAttributedString( + attributedString: attributedText ?? NSAttributedString() + ) + let textColor = textColor ?? .black + + for word in words { + let range = (currentText as NSString).range(of: word) + + if range.location != NSNotFound { + let highlightedAttributes: [NSAttributedString.Key: Any] = [ + .font: UIFont.pretendard(style), + .foregroundColor: color ?? textColor + ] + mutableAttributedString.addAttributes(highlightedAttributes, range: range) + attributedText = mutableAttributedString + } + } + } +} diff --git a/KkuMulKum/Resource/Extension/UIStackView+.swift b/KkuMulKum/Resource/Extension/UIStackView+.swift new file mode 100644 index 00000000..2a3f3baa --- /dev/null +++ b/KkuMulKum/Resource/Extension/UIStackView+.swift @@ -0,0 +1,21 @@ +// +// UIStackView+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension UIStackView { + convenience init(axis: NSLayoutConstraint.Axis) { + self.init(frame: .zero) + self.axis = axis + } + + func addArrangedSubviews(_ views: UIView...) { + views.forEach { + self.addArrangedSubview($0) + } + } +} diff --git a/KkuMulKum/Resource/Extension/UITextField+.swift b/KkuMulKum/Resource/Extension/UITextField+.swift new file mode 100644 index 00000000..f7a51af5 --- /dev/null +++ b/KkuMulKum/Resource/Extension/UITextField+.swift @@ -0,0 +1,52 @@ +// +// UITextField+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension UITextField { + func addPadding(left: CGFloat? = nil, right: CGFloat? = nil) { + if let left { + leftView = UIView(frame: CGRect(x: 0, y: 0, width: left, height: 0)) + leftViewMode = .always + } + + if let right { + rightView = UIView(frame: CGRect(x: 0, y: 0, width: right, height: 0)) + rightViewMode = .always + } + } + + func setText( + placeholder: String, + textColor: UIColor, + backgroundColor: UIColor, + placeholderColor: UIColor, + style: UIFont.Pretendard + ) { + self.textColor = textColor + self.backgroundColor = backgroundColor + attributedPlaceholder = NSAttributedString( + string: placeholder, + attributes: [.foregroundColor: placeholderColor, .font: style, .kern: style.tracking] + ) + self.attributedText = .pretendardString(style: style) + } + + func setAutoType( + autocapitalizationType: UITextAutocapitalizationType = .none, + autocorrectionType: UITextAutocorrectionType = .no + ) { + self.autocapitalizationType = autocapitalizationType + self.autocorrectionType = autocorrectionType + } + + func setLayer(borderWidth: CGFloat = 0, borderColor: UIColor, cornerRadius: CGFloat) { + layer.borderColor = borderColor.cgColor + layer.cornerRadius = cornerRadius + layer.borderWidth = borderWidth + } +} diff --git a/KkuMulKum/Resource/Extension/UIView+.swift b/KkuMulKum/Resource/Extension/UIView+.swift new file mode 100644 index 00000000..46deccd1 --- /dev/null +++ b/KkuMulKum/Resource/Extension/UIView+.swift @@ -0,0 +1,27 @@ +// +// UIView+.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +extension UIView { + convenience init(backgroundColor: UIColor) { + self.init(frame: .zero) + self.backgroundColor = backgroundColor + } + + func addSubviews(_ views: UIView...) { + views.forEach { + addSubview($0) + } + } + + func roundCorners(cornerRadius: CGFloat, maskedCorners: CACornerMask) { + clipsToBounds = true + layer.cornerRadius = cornerRadius + layer.maskedCorners = CACornerMask(arrayLiteral: maskedCorners) + } +} diff --git a/KkuMulKum/Resource/Util/ReuseIdentifiable.swift b/KkuMulKum/Resource/Util/ReuseIdentifiable.swift new file mode 100644 index 00000000..8c261e99 --- /dev/null +++ b/KkuMulKum/Resource/Util/ReuseIdentifiable.swift @@ -0,0 +1,14 @@ +// +// ReuseIdentifiable.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import Foundation + +protocol ReuseIdentifiable {} + +extension ReuseIdentifiable { + static var reuseIdentifier: String { String(describing: self) } +} diff --git a/KkuMulKum/Resource/Util/Screen.swift b/KkuMulKum/Resource/Util/Screen.swift new file mode 100644 index 00000000..a0e4b9b6 --- /dev/null +++ b/KkuMulKum/Resource/Util/Screen.swift @@ -0,0 +1,22 @@ +// +// Screen.swift +// KkuMulKum +// +// Created by 김진웅 on 6/30/24. +// + +import UIKit + +enum Screen { + static func width(_ value: CGFloat) -> CGFloat { + let screenWidth = UIScreen.main.bounds.width + let designWidth: CGFloat = 375.0 + return screenWidth / designWidth * value + } + + static func height(_ value: CGFloat) -> CGFloat { + let screenHeight = UIScreen.main.bounds.height + let designHeight: CGFloat = 812.0 + return screenHeight / designHeight * value + } +}