Swift

Swift - CollectionView - ์„ ํƒํ•œ ์…€ UI ๋ณ€๊ฒฝ

iosos 2024. 11. 11. 17:02

๐Ÿ“‘ ํ•™์Šต ๋‚ด์šฉ

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
        }
    }

๐Ÿ“š ์ฐธ๊ณ  ์ž๋ฃŒ

 

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