๐ ํ์ต ๋ด์ฉ
1. ์ ํ๋ ์ ์ ์ธ๋ฑ์ค ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ ๋ณ์ ์ถ๊ฐ
-
var selectedItemIndex : Int?
2. didSelectItemAt ๋ฉ์๋์์ ์ ํ๋ ์ ์ ์ธ๋ฑ์ค ์ ๋ฐ์ดํธ ํ reloadData() ํธ์ถ
.
.
.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedItemIndex = indexPath.row
collectionView.reloadData()
}
3. cellForItemAt ๋ฉ์๋์์ ์ ์ฅ๋ ์ธ๋ฑ์ค๋ก UI ๋ณ๊ฒฝ
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PurchaseCollectionViewCell.identifier, for: indexPath) as? PurchaseCollectionViewCell else {
return UICollectionViewCell()
}
let _ = item.sizes?[indexPath.row].map { size, price in
cell.config(size: size, price: price)
}
if indexPath.row == selectedItemIndex {
cell.setBoldCell()
} else {
cell.setBasicCell()
}
return cell
}
→ UI ๋ณ๊ฒฝ ์ฝ๋(PurchaseCollectionViewCell ๋ด๋ถ)
func setBoldCell() {
boldLabel(label: lblSize, size: 14, isBold: true)
boldLabel(label: lblPrice, size: 12, isBold: true)
borderView(isBold: true)
}
func setBasicCell() {
boldLabel(label: lblSize, size: 14, isBold: false)
boldLabel(label: lblPrice, size: 12, isBold: false)
borderView(isBold: false)
}
private func boldLabel(label : UILabel, size : CGFloat, isBold : Bool){
if isBold {
// ๊ธฐ์กด ์์ฑ์ ์ ์งํ๊ณ font๋ง ๋ณ๊ฒฝ
if let attributedText = label.attributedText {
let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
mutableAttributedText.addAttribute(.font, value: UIFont.systemFont(ofSize: size, weight: .bold), range: NSRange(location: 0, length: mutableAttributedText.length))
label.attributedText = mutableAttributedText
}
} else {
// ๊ธฐ์กด ์์ฑ์ ์ ์งํ๊ณ font๋ง ๋ณ๊ฒฝ
if let attributedText = label.attributedText {
let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
mutableAttributedText.addAttribute(.font, value: UIFont.systemFont(ofSize: size), range: NSRange(location: 0, length: mutableAttributedText.length))
label.attributedText = mutableAttributedText
}
}
}
private func borderView(isBold : Bool) {
if isBold {
grpView.layer.borderColor = UIColor.black.cgColor
} else {
grpView.layer.borderColor = UIColor(hex: "#F2F2F2")?.cgColor
}
}
๐ ์ฐธ๊ณ ์๋ฃ
- https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618032-collectionview
- https://ios-development.tistory.com/359
- https://developer.apple.com/documentation/foundation/nsmutableattributedstring
NSMutableAttributedString | Apple Developer Documentation
A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.
developer.apple.com
[iOS - swift] attributedText, NSMutableAttributedString (์ํ๋ ๋ฌธ์์ด ์์ฑ ์ง์ , ๋ฌธ์์ด attributedText ๋ฐํ)
๋ณํํ - ํน์ ๋ฌธ์์ด์ ์ ๋ ฅํ๋ฉด ๊ทธ ๋ฌธ์์ด๋ง bold๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ UILabel extension์ผ๋ก ์ ์ extension UILabel { func bold(targetString: String) { let fontSize = self.font.pointSize let font = UIFont.boldSystemFont(ofSize: fontSize
ios-development.tistory.com
collectionView(_:didSelectItemAt:) | Apple Developer Documentation
Tells the delegate that the item at the specified index path was selected.
developer.apple.com
'Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift - ์นด์นด์ค ๋ก๊ทธ์ธ ์ค๋ฅ(์น ๊ด๋ฆฌ์ ์ค์ ์ค๋ฅ, KOE101) ํด๊ฒฐ (0) | 2024.11.16 |
---|---|
Swift - ์นด์นด์ค ๋ก๊ทธ์ธ ๊ตฌํ (1) | 2024.11.13 |
Swift - Tab Bar ์์ ํ ์จ๊ธฐ๊ธฐ(hidesBottomBarWhenPushed ์์ฑ) (0) | 2024.11.11 |
Swift - UIView ์๋จ์๋ง ํ ๋๋ฆฌ(border) ๋ฃ๊ธฐ (0) | 2024.11.04 |
Swift - viewDidLayoutSubviews() (0) | 2024.10.20 |