Skip to content

Commit

Permalink
add/#89 Extension 및 Util 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JinUng41 committed Jun 30, 2024
1 parent ad43350 commit 607ade1
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 0 deletions.
26 changes: 26 additions & 0 deletions KkuMulKum/Resource/Extension/NSAttributedString+.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
31 changes: 31 additions & 0 deletions KkuMulKum/Resource/Extension/UIButton+.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
45 changes: 45 additions & 0 deletions KkuMulKum/Resource/Extension/UIFont+.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
}
37 changes: 37 additions & 0 deletions KkuMulKum/Resource/Extension/UILabel+.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
21 changes: 21 additions & 0 deletions KkuMulKum/Resource/Extension/UIStackView+.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
52 changes: 52 additions & 0 deletions KkuMulKum/Resource/Extension/UITextField+.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
27 changes: 27 additions & 0 deletions KkuMulKum/Resource/Extension/UIView+.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
14 changes: 14 additions & 0 deletions KkuMulKum/Resource/Util/ReuseIdentifiable.swift
Original file line number Diff line number Diff line change
@@ -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) }
}
22 changes: 22 additions & 0 deletions KkuMulKum/Resource/Util/Screen.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 607ade1

Please sign in to comment.