Skip to content

Commit

Permalink
fix/#286 빈 문자열 시, 런타임 에러가 발생하는 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
JinUng41 committed Aug 13, 2024
1 parent 5e2834d commit 754e89f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 42 deletions.
17 changes: 8 additions & 9 deletions KkuMulKum/Resource/Extension/UILabel+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import UIKit

extension UILabel {
func setText(
_ text: String,
_ text: String = " ",
style: UIFont.Pretendard,
color: UIColor = .black,
isSingleLine: Bool = false
) {
attributedText = .pretendardString(text, style: style)
attributedText = .pretendardString(text.isEmpty ? " " : text, style: style)
textColor = color
if isSingleLine {
numberOfLines = 1
Expand All @@ -24,23 +24,22 @@ extension UILabel {
}
}

func updateText(_ text: String) {
func updateText(_ text: String?) {
guard let currentAttributes = attributedText?.attributes(at: 0, effectiveRange: nil) else {
self.text = text
return
}
attributedText = NSAttributedString(string: text, attributes: currentAttributes)
attributedText = NSAttributedString(string: text ?? " ", attributes: currentAttributes)
}

func setHighlightText(_ words: String..., style: UIFont.Pretendard, color: UIColor? = nil) {
guard let currentText = attributedText?.string else { return }
let mutableAttributedString = NSMutableAttributedString(
attributedString: attributedText ?? NSAttributedString()
)
guard let currentAttributedText = attributedText else { return }

let mutableAttributedString = NSMutableAttributedString(attributedString: currentAttributedText)
let textColor = textColor ?? .black

for word in words {
let range = (currentText as NSString).range(of: word)
let range = (currentAttributedText.string as NSString).range(of: word)

if range.location != NSNotFound {
let highlightedAttributes: [NSAttributedString.Key: Any] = [
Expand Down
24 changes: 6 additions & 18 deletions KkuMulKum/Source/AddPromise/Cell/PlaceListCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class PlaceListCell: BaseCollectionViewCell {
static let defaultHeight = Screen.height(118)

private let titleLabel = UILabel().then {
$0.setText("", style: .body05, color: .gray8)
$0.setText(style: .body05, color: .gray8)
}

private let roadAddressTitleLabel = UILabel().then {
Expand All @@ -28,7 +28,7 @@ final class PlaceListCell: BaseCollectionViewCell {
}

private let roadAddressNameLabel = UILabel().then {
$0.setText(" ", style: .caption02, color: .gray6)
$0.setText(style: .caption02, color: .gray6, isSingleLine: true)
}

private let addressTitleLabel = UILabel().then {
Expand All @@ -41,7 +41,7 @@ final class PlaceListCell: BaseCollectionViewCell {
}

private let addressNameLabel = UILabel().then {
$0.setText(" ", style: .caption02, color: .gray6)
$0.setText(style: .caption02, color: .gray6, isSingleLine: true)
}


Expand Down Expand Up @@ -116,20 +116,8 @@ extension PlaceListCell {

private extension PlaceListCell {
func configureCell(title: String, roadAddress: String?, address: String?) {
titleLabel.setText(title, style: .body05, color: .gray8)

roadAddressNameLabel.setText(
roadAddress ?? " ",
style: .caption02,
color: .gray6,
isSingleLine: true
)

addressNameLabel.setText(
address ?? " ",
style: .caption02,
color: .gray6,
isSingleLine: true
)
titleLabel.updateText(title)
roadAddressNameLabel.updateText(roadAddress)
addressNameLabel.updateText(address)
}
}
2 changes: 1 addition & 1 deletion KkuMulKum/Source/AddPromise/Cell/SelectMemberCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension SelectMemberCell {
func configure(with member: Member) {
self.member = member

nameLabel.setText(member.name ?? " ", style: .body06, color: .gray6)
nameLabel.updateText(member.name)

let imageURL = member.profileImageURL.flatMap { URL(string: $0) }
profileImageView.kf.setImage(with: imageURL, placeholder: UIImage.imgProfile.withRenderingMode(.alwaysOriginal))
Expand Down
6 changes: 3 additions & 3 deletions KkuMulKum/Source/MeetingInfo/Cell/MeetingMemberCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class MeetingMemberCell: BaseCollectionViewCell {
}

private let nameLabel = UILabel().then {
$0.setText(" ", style: .caption02, color: .gray6)
$0.setText(style: .caption02, color: .gray6)
$0.textAlignment = .center
}

Expand All @@ -38,7 +38,7 @@ final class MeetingMemberCell: BaseCollectionViewCell {
$0.isEnabled = false
}

nameLabel.setText(" ", style: .caption02, color: .gray6)
nameLabel.setText(style: .caption02, color: .gray6)
}

override func setupView() {
Expand Down Expand Up @@ -94,7 +94,7 @@ private extension MeetingMemberCell {
$0.isEnabled = true
}

nameLabel.setText(" ", style: .caption02, color: .gray6)
nameLabel.setText(style: .caption02, color: .gray6)
}

func configureForProfile(with member: Member) {
Expand Down
16 changes: 8 additions & 8 deletions KkuMulKum/Source/MeetingInfo/Cell/MeetingPromiseCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Then

final class MeetingPromiseCell: BaseCollectionViewCell {
private let dDayLabel = UILabel().then {
$0.setText("", style: .body05, color: .mainorange)
$0.setText(style: .body05, color: .mainorange)
}

private let nameLabel = UILabel().then {
Expand All @@ -26,7 +26,7 @@ final class MeetingPromiseCell: BaseCollectionViewCell {
}

private let dateLabel = UILabel().then {
$0.setText("", style: .body06, color: .gray7)
$0.setText(style: .body06, color: .gray7)
}

private let dateStackView = UIStackView(axis: .horizontal).then {
Expand All @@ -40,7 +40,7 @@ final class MeetingPromiseCell: BaseCollectionViewCell {
}

private let timeLabel = UILabel().then {
$0.setText("", style: .body06, color: .gray7)
$0.setText(style: .body06, color: .gray7)
}

private let timeStackView = UIStackView(axis: .horizontal).then {
Expand All @@ -54,7 +54,7 @@ final class MeetingPromiseCell: BaseCollectionViewCell {
}

private let placeLabel = UILabel().then {
$0.setText("", style: .body06, color: .gray7)
$0.setText(style: .body06, color: .gray7)
}

private let placeStackView = UIStackView(axis: .horizontal).then {
Expand Down Expand Up @@ -110,9 +110,9 @@ extension MeetingPromiseCell {
func configure(dDay: Int, name: String, date: String, time: String, place: String) {
let dDayText = dDay == 0 ? "DAY" : "\(dDay)"
dDayLabel.setText("D-\(dDayText)", style: .body05, color: dDay == 0 ? .mainorange : .gray5)
nameLabel.setText(name, style: .body03, color: .gray8)
dateLabel.setText(date, style: .body06, color: .gray7)
timeLabel.setText(time, style: .body06, color: .gray7)
placeLabel.setText(place, style: .body06, color: .gray7, isSingleLine: true)
nameLabel.updateText(name)
dateLabel.updateText(date)
timeLabel.updateText(time)
placeLabel.updateText(place)
}
}
4 changes: 2 additions & 2 deletions KkuMulKum/Source/MeetingInfo/View/MeetingInfoBannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ final class MeetingInfoBannerView: BaseView {

extension MeetingInfoBannerView {
func configure(createdAt: String, metCount: Int) {
createdAtLabel.setText(createdAt, style: .label01, color: .gray4)
createdAtLabel.updateText(createdAt)

metCountLabel.do {
$0.setText("이 모임의 꾸물이들은 \(metCount)번 만났어요!", style: .body03, color: .gray8)
$0.updateText("이 모임의 꾸물이들은 \(metCount)번 만났어요!")
$0.setHighlightText("\(metCount)", style: .body03, color: .maincolor)
}
}
Expand Down
2 changes: 1 addition & 1 deletion KkuMulKum/Source/MeetingInfo/View/MeetingInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ extension MeetingInfoView {

func configureMemberCount(_ memberCount: Int) {
memberCountLabel.do {
$0.setText("모임 참여 인원 \(memberCount)", style: .body01, color: .gray8)
$0.updateText("모임 참여 인원 \(memberCount)")
$0.setHighlightText("\(memberCount)", style: .body01, color: .maincolor)
}
}
Expand Down

0 comments on commit 754e89f

Please sign in to comment.