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

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

[UIKit] UIButton์˜ invalidateIntrinsicContentSize()๊ณผ Configuration ๋ถ„๊ธฐ์ฒ˜๋ฆฌ! (UIButton๊ณผ์˜ ์ „์Ÿ)

๊ฐ์ž ๐Ÿฅ” 2022. 11. 4. 03:05
๋ฐ˜์‘ํ˜•

์‚ฌ์‹ค๋Œ€๋กœ ๋งํ•˜์ž๋ฉด, ์ด ์ „์Ÿ์€ 11์›” 1์ผ ~ 11์›” 3์ผ๊นŒ์ง€ ์ดํ‹€์— ๊ฑธ์ณ ๋ฐœ์ƒํ–ˆ๋‹ค. 
๋จธ๋ฆฌ๊ฐ€ ์•ˆ๋Œ์•„๊ฐ€์„œ ์ด๊ฑฐ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ๋‹ค๋ฅธ์ž‘์—…ํ•˜๋‹ค๊ฐ€ ํ–ˆ๋‹ค.! ๐Ÿคฏ ์ง„์งœ ์ด๊ฑฐ์ฒ˜๋Ÿผ ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์กŒ์—ˆ์Œ ;; 

 

 

๐Ÿงจ 1์ฐจ ์ „์Ÿ ๋ฐœ๋ฐœ - button์˜ width๋Š” ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•˜๋Š”๊ฑฐ๋ƒ๊ณ  ;

1๏ธโƒฃ UIButton์€ incristic? content ์‚ฌ์ด์ฆˆ๋ฅผ ๋”ฐ๋ผ์„œ width์™€ hegith๊ฐ€ ์ž๋™์œผ๋กœ ๊ฒฐ์ •๋˜๋Š”๋ฐ ์™œ ์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๋Š”๊ฑฐ์ง€?

1์ฐจ ์‹œ๋„

  • ์ธํ„ฐ๋„ท์—์„œ ๋ฒ„ํŠผ ํฐํŠธ์™€ ๋ฒ„ํŠผํฌ๊ธฐ๋ฅผ ๋งž์ถ”๋Š” ์ž‘์—…์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋ฅผ ๋‚˜๋ˆ„๋Š” ๊ธ€์„ ๋ดค๋Š”๋ฐ, ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์•Œ๋ ค์ฃผ๋”๋ผ๊ณ ?
self.titleLabel?.adjustsFontSizeToFitWidth = true

  • ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š”, ‘์ˆ˜์ •ํ•˜๋Ÿฌ๊ฐ€๊ธฐ’๋ผ๋Š” ํฐํŠธ๊ฐ€ ์ž‘์•„์ ธ๋ฒ„๋ฆฌ๋ฉด์„œ ๋ฒ„ํŠผ ๋‚ด๋ถ€๋กœ๋“ค์–ด์™€๋ฒ„๋ฆฐ๋‹ค… ใ…‚ใ„ทใ…‚ใ„ท๐Ÿ˜ก
  • width์— ๋งž์ถฐ์„œ font size๋ฅผ ๋ฐ”๊ฟ”๋ฒ„๋ฆฌ๋Š” ์ฝ”๋“œ๋ผ์„œ ๊ทธ๋ ‡๋‹ค๊ณ  ํ•œ๋‹ค.

adjustsFontSizeToFitWidth์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์ž!

https://developer.apple.com/documentation/uikit/uilabel/1620546-adjustsfontsizetofitwidth

๐ŸŽ width์— ๋งž๊ฒŒ ๊ธ€๊ผด ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•œ๋‹ค.
๋ ˆ์ด๋ธ”์ด ๋ ˆ์ด๋ธ”์˜ ๊ฒฝ๊ณ„ ์‚ฌ๊ฐํ˜•์— ์ œ๋ชฉ ๋ฌธ์ž์—ด์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ํ…์ŠคํŠธ์˜ ๊ธ€๊ผด ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” Bool๊ฐ’์ด๋‹ค.

์•„ ์ •์˜์—์„œ ์™„์ „ํžˆ ๊ทธ๋ƒฅ ๊ธ€๊ผด์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•œ๋‹ค๊ณ  ๋‚˜์™€์žˆ๊พธ๋‚˜!!!!!

 

2์ฐจ ์งˆ๋ฌธ - ์˜์‹ฌ๋‹จ๊ณ„

  • ๋ฒ„ํŠผ์„ ์„ค์ •ํ• ๋•Œ width๋ฅผ ๊ฐ•์ œ๋กœ ์ฃผ๋Š”๊ฑฐ ์•„๋‹ˆ๋ƒ๋ฉด์„œ ๋“€๋‚˜์„ ์ƒ๋‹˜์—๊ฒŒ ์งˆ๋ฌธ
    • ๊ฐ•์ œ๋กœ ๊ทธ๋ƒฅ ์ฃผ๊ณ ๋งŒ ์‹ถ์—ˆ๋‹ค ;;
  • ๋“€๋‚˜์„ ์ƒ๋‹˜ ๋‹ต๋ณ€์œผ๋กœ๋Š”, ๋ฒ„ํŠผ์„ ์„ค์ •ํ• ๋•Œ ์ฃผ๋กœ width๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ฑฐ๋‚˜ leading+trailing์„ ์ค˜์„œ ํฌ๊ธฐ๋ฅผ ์ฃผ์–ด์ฃผ๋Š” ํŽธ์ด๋ผ๊ณ  ํ•œ๋‹ค!!! ํ˜ธ๋ค ์ด์ด์ด์ด ํ˜ธ๋ค ์ด์ด ๐ŸŽŠ
    • ๊ผญ ํฌ๊ธฐ๋ฅผ ์žก์•„์ฃผ๋Š” ์ด์œ ๋Š”, width๊ฐ€ ์ •ํ™•ํ•˜์ง€ ์•Š์œผ๋ฉด frame์ด ๋ช‡์ธ์ง€ ๋ชฐ๋ผ์„œ ๋ชจ์–‘์ด ์•ˆ์žกํ˜€์„œ ํ„ฐ์น˜๊ฐ€ ์•ˆ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

→ ์˜ค์ผ€์ด ๊ทธ๋Ÿผ ์ผ๋‹จ width๋Š” ์žก์•„์ค˜์•ผํ•˜๋Š”๊ฑฐ ํ™•์ธ.

slack... ์งฑ

 

3์ฐจ - ๋“œ๋””์–ด ์ง๊ฟ์„ ๋งŒ๋‚ฌ๋‹ค

๋Œ์•„๋‹ค๋‹ˆ๋ฉด์„œ ํ•ด๋‹น ์ฝ”๋“œ ๋ฐœ๊ฒฌ. ์ด๊ฒƒ์„ ๋„ฃ์–ด์ฃผ๋‹ˆ๊นŒ, button ๋‚ด๋ถ€ ๊ธ€์ž๋“ค์— ๋งž์ถฐ์„œ button width๊ฐ€ ์ž๋™์œผ๋กœ ๋Š˜์–ด๋‚˜๋”๋ผ๊ณ !!! ๐ŸŽŠ๐ŸŽŠ๐ŸŽŠ… ๋‚ด๊ฐ€ ์ฐพ๋˜๊ฒŒ ๋ฐ”๋กœ ์ด๋Ÿฐ๊ฑฐ๋ผ๊ตฟ ๐Ÿ‘ป

self.invalidateIntrinsicContentSize()

invalidateIntrinsicContentSize()๋ž€?

https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize

์ฝ์–ด๋ณด๋‹ˆ๊นŒ, ๊ณ ์œ  ์ฝ˜ํ…์ธ ์˜ ํฌ๊ธฐ๋ฅผ ๋ฌดํšจํ™”ํ•˜๋Š” ์ฝ”๋“œ๋ผ๊ณ  ํ•œ๋‹ค. ๋ฒ„ํŠผ๋„ label์ด ๊ธธ์–ด์ง€๋ฉด ๋ง์ค„์ž„ํ‘œ๋กœ ๋ผ๋ฒจ์ด ์ค„์–ด๋“ค์—ˆ๋Š”๋ฐ, ์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ˆ๊นŒ ๊ณ ์œ  ์ปจํ…์ธ  (button)์˜ ํฌ๊ธฐ๋ฅผ ๋ฌดํšจํ™”์‹œ์ผœ๋ฒ„๋ ค์„œ ๊ธ€์ž์— ๋งž์ถฐ์„œ ๋ฒ„ํŠผ์˜ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง€๋Š” ๊ฑฐ ๊ฐ™์•˜๋‹ค. ๊ทธ๋ž˜์„œ ํ•ด๊ฒฐํ•œ ํ™”๋ฉด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ด๋ ‡๊ฒŒ ๋”ฑ ๋งž์ถฐ์„œ ๋ฒ„ํŠผ์ด ์ •ํ•ด์ง€๋Š” ๊ฒƒ! ์ด์ œ๋Š” ๋‚ด๋ถ€ image+label์— inset์„ ๋„ฃ์–ด์ฃผ์–ด์„œ ๋””์ž์ด๋„ˆ๊ฐ€ ์ „๋‹ฌํ•ด์ค€ ๋ฒ„ํŠผ ๋ชจ์–‘๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๐Ÿงจ 2์ฐจ ์ „์Ÿ - ์™œ inset ์ ์šฉ ์•ˆ๋˜๋ƒ๊ณ  ;;

2๏ธโƒฃ ๋ฒ„์ „์—๋”ฐ๋ผ์„œ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ–‡๋Š”๋ฐ, ์™œ ์ ์šฉ์ด ์•ˆ๋˜๋Š”๊ฑฐ์•ผ?

์›๋ž˜๋Š” ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ์ด๋ ‡๊ฒŒ ํ–ˆ์—ˆ๋‹ค

private func configureUI() {
    self.invalidateIntrinsicContentSize()
    self.titleLabel?.textAlignment = .center
    self.backgroundColor = .evyBlack1
    self.imageView?.contentMode = .scaleAspectFit
    self.constraint(.heightAnchor, constant: Size.buttonHeigth)

    // TODO: - 15๋ฒ„์ ผ์—์„œ contentinset๋„ฃ์–ด์•ผํ•˜๊ณ , cornerradius๋„ฃ์–ด์•ผํ•จ
    if #available(iOS 15.0, *) {
        configuration?.preferredSymbolConfigurationForImage = Size.buttonImageSize
    } else {
        self.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 30)
    }
}

๋จผ์ € ๋ฒ„ํŠผ์˜ ๋ชจ์–‘์„ ์ „๋ถ€ ์žก์•„์ค€ ๋‹ค์Œ, configuration ์ฝ”๋“œ๋ฅด ๊ผญ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋‹ค. ์ž๊พธ๋งŒ ์›ํ•˜๋Š” ๋ชจ์–‘์ด ๋‚˜์˜ค์ง€ ์•Š๊ธธ๋ž˜ print๋ฌธ๊ณผ .capsule๋กœ Corner ๋ชจ์–‘์„ ๋ฐ”๊ฟ”์ค˜ ๋ณด์•˜๋‹ค.

private func configureUI() {
    self.invalidateIntrinsicContentSize()
    self.titleLabel?.textAlignment = .center
    self.backgroundColor = .evyBlack1
    self.imageView?.contentMode = .scaleAspectFit
    self.constraint(.heightAnchor, constant: Size.buttonHeigth)

    // TODO: - 15๋ฒ„์ ผ์—์„œ contentinset๋„ฃ์–ด์•ผํ•˜๊ณ , cornerradius๋„ฃ์–ด์•ผํ•จ
    if #available(iOS 15.0, *) {
        configuration?.cornerStyle = .capsule
        configuration?.preferredSymbolConfigurationForImage = Size.buttonImageSize
        print("์—ฌ๊ธธ์ง€๋‚˜๊ธดํ•ด?")
    } else {
        self.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 30)
    }
}

์ด๋ ‡๊ฒŒ ํ•ด๋ณด๋‹ˆ๊นŒ, ์ผ๋‹จ, ์ € ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ธดํ•œ๋‹ค.. ๊ทผ๋ฐ cornerStyle์ด ๋ณ€ํ•˜์ง€ ์•Š์•˜๋‹ค. ์ฆ‰, ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ธด ํ•˜๋Š”๋ฐ configuration์— ์ ์šฉํ•œ ์†์„ฑ๋“ค์ด ์ ์šฉ๋˜์ง€ ์•Š๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ง€…

๋„๋Œ€์ฒด ์™œ๊ทธ๋Ÿด๊นŒ????? ์šฐ์„  ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ cusom UIButton์„ ๋งŒ๋“  ์‚ฌ๋žŒ๋“ค์„ ์ฐพ์•„๋‚˜์„ฐ๋‹ค… ๊ตฌ๊ธ€๋ง๋„ ์‹ค๋ ฅ์ด๋‹ค….

๊ตฌ๊ธ€๋ง ๊ฒฐ๊ณผ ์•„๋ž˜ ๋ธ”๋กœ๊ทธ์—์„œ๋Š”, ๋ฒ„ํŠผ์— ์ ์šฉ๋˜๋Š” ์†์„ฑ๋“ค ๋ชจ๋‘์ „๋ถ€ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ๋‹ค.

https://minios.tistory.com/6

 

[UIKit] UIButton์˜ Image, Title ํฌ๊ธฐ ์กฐ์ ˆํ•˜๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š”. miniOS์ž…๋‹ˆ๋‹ค! TIL์€ ํ‰์–ด์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. UI๋ฅผ Code๋กœ๋งŒ ๊ตฌํ˜„ํ•ด๋ณด๋Š” ์—ฐ์Šต์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์œ„์™€ ๊ฐ™์€ ํ™”๋ฉด์„ ๋”ฐ๋ผ์„œ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค. ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์˜ ๋ฒ„ํŠผ๋“ค์€ Image๋งŒ์„ ๊ฐ–

minios.tistory.com

์ด๋ ‡๊ฒŒ ๋ง์ด๋‹ค,,,,

์•„์ฐจ์ฐจ..? ๐Ÿค” ๋‹น์—ฐํžˆ configuration์œผ๋กœ ์ ์šฉ๋˜๋Š” ๋ฒ„ํŠผ์€ configuration์œผ๋กœ๋งŒ ์†์„ฑ์ด ์ ์šฉ๋  ๊ฒƒ์ธ๋ฐ, ๋‚ด๊ฐ€ configuration์œผ๋กœ ์ฃผ์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ด ๋ฌธ์ œ์˜€๋˜๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋„ ์œ„ ์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด์•˜๋‹ค.

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

private func configureUI() {
        if #available(iOS 15.0, *) {
            var buttonConfig = UIButton.Configuration.filled()
            buttonConfig.titleAlignment = .leading
            buttonConfig.baseBackgroundColor = .evyBlack1
            buttonConfig.cornerStyle = .capsule
            buttonConfig.preferredSymbolConfigurationForImage = Size.buttonImageSize
            buttonConfig.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 10, bottom: 8, trailing: 30)
            configuration = buttonConfig
            print("์—ฌ๊ธธ์ง€๋‚˜๊ธดํ•ด?")
        } else {
            self.invalidateIntrinsicContentSize()
            self.titleLabel?.textAlignment = .center
            self.backgroundColor = .evyBlack1
            self.imageView?.contentMode = .scaleAspectFit
            self.constraint(.heightAnchor, constant: Size.buttonHeigth)
            self.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 30)
        }
    }

 

์™€์šฐ ์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ !!! 15๋ฒ„์ „์œผ๋กœ ๋นŒ๋“œํ–ˆ์„๋•Œ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ถœ๋ ฅ์ด ๋œ๋‹ค!!!!

 

์œ„ ์‚ฌ์ง„์ด 15๋ฒ„์ „ ์ด์ƒ์ผ ๊ฒฝ์šฐ์ด๊ณ , ์•„๋ž˜ ์‚ฌ์ง„์ด 15๋ฒ„์ „ ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ์ด๋‹ค.

์ฝ”๋“œ๋ฅผ๋ณด๋ฉด, 15๋ฒ„์ „์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋งŒ cornerStyle์„ ๊ฑธ์–ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— corner๊ฐ€ ๋‘ฅ๊ทธ๋ž—๊ฒŒ ๋‚˜์˜ค๊ณ , 15๋ฏธ๋งŒ์ธ ๋ฒ„์ „์—๋Š” ์•„์ง cornerRadius๊ด€๋ จํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋„ค๋ชจ๋‚ณ๊ฒŒ ๋‚˜์˜จ๋‹ค.

์ด๋กœ์จ ๋‚˜๋Š”, ๋ฒ„ํŠผ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ์— ์„ฑ๊ณตํ–ˆ๋”ฐ!!!!! ์ด๊ฑธ๋กœ ์ดํ‹€ ๊ณ ์ƒํ•œ๊ฑด ์•ˆ๋น„๋ฐ€ ์‰ฟ

๋ฐ˜์‘ํ˜•