Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class BalloonBackgroundView: UIView {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .clear
collectionView.isScrollEnabled = false
collectionView.register(BalloonChipCell.self, forCellWithReuseIdentifier: BalloonChipCell.identifier)
collectionView.register(BalloonChipCell.self, forCellWithReuseIdentifier: BalloonChipCell.identifiers)
return collectionView
}()

Expand Down Expand Up @@ -289,7 +289,7 @@ extension BalloonBackgroundView: UICollectionViewDataSource {
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: BalloonChipCell.identifier,
withReuseIdentifier: BalloonChipCell.identifiers,
for: indexPath
) as? BalloonChipCell,
let input = tagSection?.inputDataList[indexPath.item]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,131 @@
import SnapKit
import UIKit

import Infrastructure

import SnapKit
import Then

final class BalloonChipCell: UICollectionViewCell {
static let identifier = "BalloonChipCell"

private let button: PPButton = {
let button = PPButton(
style: .secondary,
text: "",
font: .korFont(style: .medium, size: 11),
cornerRadius: 15
)

button.titleLabel?.lineBreakMode = .byTruncatingTail
button.titleLabel?.adjustsFontSizeToFitWidth = false
return button
}()

override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(button)
setupLayout()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupLayout() {
button.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}

func configure(with title: String, isSelected: Bool) {
button.setTitle(title, for: .normal)
if isSelected {
let checkImage = UIImage(named: "icon_check_white")?.withRenderingMode(.alwaysOriginal)
let resizedImage = checkImage?.resize(to: CGSize(width: 16, height: 16))
button.setImage(resizedImage, for: .normal)
button.semanticContentAttribute = .forceRightToLeft
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 0)
button.contentEdgeInsets = UIEdgeInsets(top: 4, left: 10, bottom: 6, right: 12)
button.setBackgroundColor(.blu500, for: .normal)
button.setTitleColor(.white, for: .normal)
button.layer.borderWidth = 0
button.titleLabel?.font = .korFont(style: .bold, size: 11)

} else {
button.setImage(nil, for: .normal)
button.semanticContentAttribute = .unspecified
button.imageEdgeInsets = .zero
button.contentEdgeInsets = UIEdgeInsets(top: 4, left: 12, bottom: 6, right: 12)
button.setBackgroundColor(.white, for: .normal)
button.setTitleColor(.g400, for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.g200.cgColor
button.titleLabel?.font = .korFont(style: .medium, size: 11)

}
}

private var currentAction: UIAction?

var buttonAction: (() -> Void)? {
didSet {
if let oldAction = currentAction {
button.removeAction(oldAction, for: .touchUpInside)
}

let action = UIAction { [weak self] _ in
self?.buttonAction?()
}
button.addAction(action, for: .touchUpInside)
currentAction = action
}
}

private enum Constant {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용할 값들을 Constant로 빼서 사용하는 룰!! 적용된 걸 보니 깔끔해서 좋습니다 :>

static let verticalInset: CGFloat = 6
static let selectedLeftInset: CGFloat = 10
static let normalLeftInset: CGFloat = 12
static let rightInset: CGFloat = 12
static let checkIconSize: CGSize = .init(width: 16, height: 16)
static let baselineOffset: CGFloat = -1
static let fontSize: CGFloat = 11
}

private let button = PPButton(
style: .secondary,
text: "",
font: .korFont(style: .medium, size: Constant.fontSize),
cornerRadius: 15
).then {
$0.titleLabel?.lineBreakMode = .byTruncatingTail
$0.titleLabel?.adjustsFontSizeToFitWidth = false
}

private var currentAction: UIAction?

override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(button)
setupLayout()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupLayout() {
button.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}

func configure(with title: String, isSelected: Bool) {
let attributedTitle = NSMutableAttributedString(string: title).then {
$0.addAttribute(
.baselineOffset,
value: Constant.baselineOffset,
range: NSRange(location: .zero, length: $0.length)
)
}

if isSelected {
let checkImage = UIImage(named: "icon_check_white")?.withRenderingMode(.alwaysOriginal).resize(to: Constant.checkIconSize)

button.then {
$0.setImage(checkImage, for: .normal)
$0.semanticContentAttribute = .forceRightToLeft
$0.imageEdgeInsets = .init(top: .zero, left: 1, bottom: .zero, right: .zero)
$0.contentEdgeInsets = .init(
top: Constant.verticalInset,
left: Constant.selectedLeftInset,
bottom: Constant.verticalInset,
right: Constant.rightInset
)
$0.setBackgroundColor(.blu500, for: .normal)
$0.setTitleColor(.white, for: .normal)
$0.layer.borderWidth = .zero
}

attributedTitle.addAttribute(
.font,
value: UIFont.korFont(style: .bold, size: Constant.fontSize)!,
range: NSRange(location: .zero, length: attributedTitle.length)
)
} else {
button.then {
$0.setImage(nil, for: .normal)
$0.semanticContentAttribute = .unspecified
$0.imageEdgeInsets = .zero
$0.contentEdgeInsets = .init(
top: Constant.verticalInset,
left: Constant.normalLeftInset,
bottom: Constant.verticalInset,
right: Constant.rightInset
)
$0.setBackgroundColor(.white, for: .normal)
$0.setTitleColor(.g400, for: .normal)
$0.layer.borderWidth = 1
$0.layer.borderColor = UIColor.g200.cgColor
}

attributedTitle.addAttribute(
.font,
value: UIFont.korFont(style: .medium, size: Constant.fontSize)!,
range: NSRange(location: .zero, length: attributedTitle.length)
)
}

self.button.setAttributedTitle(attributedTitle, for: .normal)
}

var buttonAction: (() -> Void)? {
didSet {
if let oldAction = currentAction {
self.button.removeAction(oldAction, for: .touchUpInside)
}

let action = UIAction { [weak self] _ in
guard let self = self else { return }
self.buttonAction?()
}

self.button.addAction(action, for: .touchUpInside)
self.currentAction = action
}
}
}

extension UIImage {
func resize(to size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
defer { UIGraphicsEndImageContext() }
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()
}
func resize(to size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
defer { UIGraphicsEndImageContext() }
self.draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()
}
}
Loading