Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

potato's iOS Story/๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋งŒ๋‚œ ์นœ๊ตฌ๋“ค

[UIKit] labelํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ CollectionView cell ํฌ๊ธฐ์— ๋„ฃ์–ด์ฃผ๊ธฐ

๊ฐ์ž ๐Ÿฅ” 2023. 2. 2. 16:06
๋ฐ˜์‘ํ˜•

๐ŸŸฃ ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ

์ง€๊ธˆ ํ•ด๋‹น ๋ทฐ์— ์žˆ๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒ„ํŠผ์€ collectionView๋กœ ๊ตฌํ˜„์ค‘์ด๋‹ค. ์ผ๋‹จ ๋‚ด๋ถ€ cell์— ์ž„์‹œ ๊ฐ’์„ ๋„ฃ์–ด์„œ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์คฌ๋‹ค.

๊ทธ๋ƒฅ ๊ธ€์ž ํฌ๊ธฐ์— ์ƒ๊ด€ ์—†์ด ์ผ์ •ํ•œ ๋ฒ„ํŠผ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ํ™”๋ฉด์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ธ€์ž ํฌ๊ธฐ์— ๋งž์ถฐ์„œ ๋ฒ„ํŠผ์˜ ํฌ๊ธฐ๋„ ๋ฐ”๋€Œ๋Š” ๊ฒƒ,, ์ด๋ ‡๊ฒŒ ํ•˜๊ณ ์‹ถ๋‹ค.

 

๐ŸŸฃ Cell์˜ ํฌ๊ธฐ๋ฅผ ์ƒ์ˆ˜๊ฐ’์œผ๋กœ ๋งŒ๋“ค๋ฉด ์•ˆ๋ผ!

๐Ÿ’ก cell ๋‚ด๋ถ€์— ๋“ค์–ด๊ฐ€๋Š” label์˜ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ cell์˜ ํฌ๊ธฐ๋กœ ์ง€์ •ํ•ด์ฃผ์ž.

์š”๋ชจ์กฐ๋ชจ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ• ๋•Œ button์„ ๋งŒ๋“ค๋•Œ ์ด๋Ÿฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์ƒ๊ฐ๋‚˜์„œ ๋ฐ”๋กœ ์‹คํ–‰์— ์˜ฎ๊ฒจ๋ดค๋‹ค.

label์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ

์•„๋ฌด๋ž˜๋„ ๋ฒ„ํŠผ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฉด์—์„œ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™์•„์„œ, size๋ฅผ ๊ตฌํ•˜๋Š” ๋งค๋‹ˆ์ €๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ทธ ๋‚ด๋ถ€์— ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ๋‹ค.

enum Frame {
	case widthAnchor, heightAnchor
}

struct CalculateSizeManager {
    func calculateCategoryButton(_ anchor: Frame, with buttonTitle: String) -> CGFloat {
        let label = UILabel()
        label.text = buttonTitle
        label.sizeToFit()

        switch anchor {
        case .widthAnchor:
            return label.frame.width + 24
        case .heightAnchor:
            return label.frame.height + 10
        }
    }
}

cell ๋‚ด๋ถ€์— text๋ฅผ ๋ณ€๊ฒฝํ•ด์ค„๋•Œ width๋ฅผ ๊ฐ™์ด ๋ณ€๊ฒฝ

cell ๋‚ด๋ถ€์— ๋“ค์–ด๊ฐ€๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์•Œ์•„์•ผ ํฌ๊ธฐ ์ง€์ •์„ ํ•  ์ˆ˜ ์žˆ์–ด์„œ setData๋ผ๋Š” ํ•จ์ˆ˜๋กœ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ๊ณ„์‚ฐํ•˜๊ณ , width๋ฅผ ๊ณ„์‚ฐํ•ด์ฃผ์—ˆ๋‹ค.

func setData(with category: String) {
    categoryLabel.text = category
    categoryLabel.constraint(.widthAnchor,
                             constant: CalculateSizeManager().calculateCategoryButton(.widthAnchor, with: category))
    categoryLabel.constraint(.heightAnchor,
                             constant: CalculateSizeManager().calculateCategoryButton(.heightAnchor, with:category))
}

๐Ÿšจ ์•„๋‹ˆ ๊ทผ๋ฐ width๋งŒ ์ง€์ •ํ–ˆ์„๋•Œ๋Š” ์ž˜ ๋‚˜์˜ค๋Š”๋ฐ, height๊นŒ์ง€ ์ ์šฉํ•˜๋ฉด, ์ด๋ ‡๊ฒŒ ํ™”๋ฉด์ด ๊นจ์ ธ๋‚˜์˜ค๊ณ , ์ฝ˜์†”์ฐฝ์ด ๋‹จ๋‹จํžˆ ํ™”๊ฐ€๋‚˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ ‡๊ฒŒ,,, ๋งจ ์•ž์— 5๋งŒํผ์˜ cell ์‚ฌ์ด ๊ฐ„๊ฒฉ์ด ์ƒ๊ธฐ๊ณ , height๊ฐ€ ๋ญ”๊ฐ€ ์ž˜๋ ค๋‚˜์˜ค๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค. ๋ฌด์Šจ์ผ์ด๋ƒ์•„์•„์•„์•„๋„๋Œ€์ฒด๊ฐ€!

โœ… ์™€ ํ•ด๊ฒฐํ• ๊ฑฐ๊ฐ™์€ ๋Š๋‚Œ์“ฐ

์ฝ˜์†”์ฐฝ์„ ๋ณด๋‹ˆ ํ™”๋‚ด๊ณ  ์žˆ๋Š” ๊ฒƒ์ค‘์— ํ•˜๋‚˜๊ฐ€ ์ด๊ฑฐ์˜€๋‹ค.

๐Ÿšซ the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

์•„! ๋†’์ด๊ฐ€ ๋ฌธ์ œ๊ตฌ๋‚˜!!

์™œ๋ƒ๋ฉด!!! ๋‚ด๊ฐ€ UICollectionView์˜ height๋ฅผ 26์œผ๋กœ ๊ณ ์ •ํ•ด๋†จ๊ธฐ ๋•Œ๋ฌธ์ด์ง€~~~ >,<๐Ÿซ โญ๏ธ

private func setupLayout() {
    view.addSubviews(descriptionLabel, categoryCollectionView)
    NSLayoutConstraint.activate([
        descriptionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
        descriptionLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),

        categoryCollectionView.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 16),
        categoryCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
        categoryCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16)
        categoryCollectionView.heightAnchor.constraint(equalToConstant: 26)
    ])
}

์งœ์ž”, ์ด๋ ‡๊ฒŒ ๋ ˆ์ด์•„์›ƒ์ด ์žกํ˜€์žˆ์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ณด๋ฉด, ๋งจ ๋งˆ์ง€๋ง‰์— height๋ฅผ 26๊ณ ์ •๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด๋‘์—ˆ๋Š”๋ฐ, ์ด๊ฒƒ ๋•Œ๋ฌธ์ธ๊ฒƒ ๊ฐ™๋‹ค. ์ˆ˜์ •ํ•ด๋ณด์ž

์—ฌ๊ธฐ์„œ ์ž ๊น, ๊ทผ๋ฐ

cell์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ collectionView์˜ height๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋Š” ์—†๋Š”๊ฑธ๊นŒ? ์˜คํ† ๋ ˆ์ด์•„์›ƒ์—์„œ ๋จผ์ € height๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค๊ณ  ๋‚˜์˜ค๋Š”๋ฐ… (์˜คํ† ๋ ˆ์ด์•„์›ƒ์œผ๋กœ height์„ค์ •์•ˆํ•ด์ฃผ๋ฉด ๋ชจํ˜ธํ•˜๋‹ค๊ณ  ๋ณด๋ผ์ƒ‰์—๋Ÿฌ๋œธ)

์ž๋™์œผ๋กœ ๋ชปํ•˜๋Š”๊ฑด๊ฐ€ใ… ใ… 

์ผ๋‹จ ์•ผ๋งค์˜ ๋ฐฉ๋ฒ•์œผ๋กœ,, calculate๋œ ๊ฐ’์„ collectionView์˜ height์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

categoryCollectionView.heightAnchor.constraint(equalToConstant: CalculateSizeManager().calculateCategoryButton(.heightAnchor, with: "temp"))

๋‚ด๊ฐ€ ๊ตฌ์„ฑํ•ด๋†“์€ CalculateCategoryButton ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” with์— ๋“ค์–ด์˜ค๋Š” ํ…์ŠคํŠธ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ ๋†’์ด๋ฅผ ์ •ํ•ด์ฃผ์—ˆ๋‹ค. ์ €๊ธฐ๋‹ค๊ฐ€ “temp” ๋„ฃ์–ด์„œ ์‚ฌ์ด์ฆˆ ์ฃผ๋Š”๊ฑฐ ๋„ˆ๋ฌด ์ด์ƒํ•œ๋ฐ,, ์‹ถ์—ˆ๋‹ค.

โœ… ์•„๋‹ˆ ๊ทธ๋Ÿผ text ๊ฐ’์„ ์ž…๋ ฅ์•ˆํ•ด๋„ ๋˜๋„๋ก ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์˜ต์…”๋„๋กœ ์ˆ˜์ •ํ•ด๋ณด์ž

์ง€๊ธˆ ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•ด๋†“์€ ์ฝ”๋“œ๋ฅด ๋ณด๋ฉด, manager์—์„œ๋Š” textStyle์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์•ฝ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ textStyle๊นŒ์ง€ ์ž…๋ ฅ๋ฐ›์•„์„œ, ๊ทธ ํฐํŠธ์˜ ํฌ๊ธฐ ๋†’์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ธธ์ด๋ฅผ ์ •ํ•ด์ฃผ๋ฉด?? ์ด๊ฑฐ๋‹ค!!

struct CalculateSizeManager {
    func calculateCategoryButton(_ anchor: Frame, with buttonTitle: String?) -> CGFloat {
        let label = UILabel()
        label.text = buttonTitle ?? "temp"
        label.sizeToFit()
        switch anchor {
        case .widthAnchor:
            return label.frame.width + 24
        case .heightAnchor:
            return label.frame.height + 8
        }
    }
}

์ด๋ ‡๊ฒŒ ์ž…๋ ฅ๋ฐ›๋Š” buttonTitle์€ ์˜ต์…”๋„๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค˜์„œ, ์˜คํ† ๋ ˆ์ด์•„์›ƒ์„ ์„ค์ •ํ•ด์ค„๋•Œ ๊ตณ์ด ‘text’๊ฐ’์ด ์—†์–ด๋„ ๋˜๊ฒŒ๋” ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋Ÿผ, collectionView์˜ ์˜คํ† ๋ ˆ์ด์•„์›ƒ์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

categoryCollectionView.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 16),
categoryCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
categoryCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
categoryCollectionView.heightAnchor.constraint(equalToConstant: CalculateSizeManager().calculateCategoryButton(.heightAnchor, with: nil))

 

์„ฑ ๊ณต..๐Ÿซถ

๋ฐ˜์‘ํ˜•