iOS

[ iOS ] UIButton.Configuration ์‚ฌ์šฉํ•˜๊ธฐ(1) - Init, Title (iOS 15)

Forest Yun 2022. 10. 13. 17:19
728x90

์ด ๊ธ€์€ iOS 15 ๋ฒ„์ „์„ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ๋œ ๊ธ€์ž…๋‹ˆ๋‹ค. 

 

 

 


 

 

 

UIButton.Configuration์ด๋ž€?

iOS 2.0+ ๋ถ€ํ„ฐ UIButton์˜ ๋ชจ์–‘์€ setTitle(:for:), setImage(:for:) ๊ณผ ๊ฐ™์€ UIButton์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ iOS 15+ ๋ถ€ํ„ฐ๋Š” UIButton.Configuration์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•์œผ๋กœ UIButton์˜ ๋ชจ์–‘๊ณผ ํ–‰๋™์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. Configuration์€ ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ๋ฉ”์„œ๋“œ๋“ค๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ๋ฉ”์„œ๋“œ๋“ค์„ ์•„์˜ˆ ๋Œ€์ฒดํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

Configuration์˜ ์žฅ์ ์€ ๊ธฐ์กด ๋ฒ„ํŠผ์˜ ๋ชจ์–‘๊ณผ ํ–‰๋™์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์ฝ”๋“œ๋ฅผ ์ผ์ผ์ด ์ˆ˜์ •ํ•  ํ•„์š”์—†์ด ์ƒˆ๋กœ์šด Configuration์„ ๊ตฌ์„ฑํ•œ ๋’ค UIButton์˜ configuration ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋ฉด ๋œ๋‹ค.

 

UIButton.Configuration ๊ณต์‹๋ฌธ์„œ

 

[ iOS ์ฝ๊ธฐ ] UIButton.Configuration

Structure UIButton.Configuration ๋ฒ„ํŠผ๊ณผ ๋ฒ„ํŠผ์˜ contents์˜ ๋ชจ์–‘๊ณผ ํ–‰๋™์„ ์ง€์ •ํ•˜๋Š” configuration(๊ตฌ์„ฑ, ์„ค์ •) iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ tvOS 15.0+ Xcode 13.0+ Declaration struct Con..

88yhtserof.tistory.com

 

UIButton์˜ configuration ํ”„๋กœํผํ‹ฐ ๊ณต์‹๋ฌธ์„œ

 

[ iOS ์ฝ๊ธฐ ] UIButton์˜ configuration ํ”„๋กœํผํ‹ฐ

Instance Property configuration The configuration for the button’s appearance.๋ฒ„ํŠผ ๋ชจ์–‘์— ๋Œ€ํ•œ configuration(๊ตฌ์„ฑ) iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ tvOS 15.0+ Xcode 13.0+ Declaration @..

88yhtserof.tistory.com

 

 

 

 

 

UIButton.Configuration์„ ์‚ฌ์šฉํ•ด ๋ฒ„ํŠผ ๊ตฌ์„ฑํ•˜๊ธฐ

 

1. Creating Configuration

iOS 15๋ฒ„์ „๋ถ€ํ„ฐ UIButton์€ ์—ฌ๋Ÿฌ ๋ชจ์–‘์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜์—ˆ๋‹ค. Configuration์—์„œ๋Š” ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์‹œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ชจ์–‘์„ ์ด๋‹ˆ์…œ๋ผ์ด์ €๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

static func plain() -> UIButton.Configuraton ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ์„ ๊ฐ€์ง„ ๋ฒ„ํŠผ
static func gray() -> UIButton.Configuraton ํšŒ์ƒ‰ ๋ฐฐ๊ฒฝ์„ ๊ฐ€์ง„ ๋ฒ„ํŠผ
static func tinted() -> UIButton.Configuraton ์›ํ•˜๋Š” ์ƒ‰์œผ๋กœ ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ
static func filled() -> UIButton.Configuraton ์›ํ•˜๋Š” ์ƒ‰์œผ๋กœ ๋ถˆํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ
static func boderless() -> UIButton.Configuraton ํ…Œ๋‘๋ฆฌ๊ฐ€ ์—†๋Š” ๋ชจ์–‘์˜ ๋ฒ„ํŠผ. plain()์œผ๋กœ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋‹ค.
static func bordered() -> UIButton.Configuraton ํ…Œ๋‘๋ฆฌ๊ฐ€ ์žˆ๋Š” ๋ชจ์–‘์˜ ๋ฒ„ํŠผ. gray()๋กœ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋‹ค.
static func borderedTinted() -> UIButton.Configuraton ์ƒ‰์กฐ์™€ ํ…Œ๋‘๋ฆฌ๊ฐ€ ์žˆ๋Š” ๋ฒ„ํŠผ. tinted()๋กœ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋‹ค.
static func borderedProminent() -> UIButton.Configuraton ๋‘๋“œ๋Ÿฌ์ง€๊ณ  ํ…Œ๋‘๋ฆฌ๊ฐ€ ์žˆ๋Š” ๋ฒ„ํŠผ. filled()๋กœ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋‹ค.

 

Configuration์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์œ„์™€ ๊ฐ™์ด ์ดˆ๊ธฐ์˜ ์ƒํƒœ๋กœ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ด๋ฏธ Configuration์ด ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š” ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

func updated(for button: UIButton) -> UIButton.Configuraton ์ฃผ์–ด์ง„ ๋ฒ„ํŠผ์— ๋Œ€ํ•ด ์—…๋ฐ์ดํŠธ๋œ Configuration์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

        var button = UIButton()
        var configuration = UIButton.Configuration.plain()
        
        button.configuration = configuration

 

 

๋˜๋Š” UIButton ์ƒ์„ฑ ์‹œ ๋ฐ”๋กœ Configuration์„ ํ• ๋‹นํ•ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

init(
    configuration: UIButton.Configuration,
    primaryAction: UIAction? = nil
)
configuration : ๋ฒ„ํŠผ์˜ configuration
primaryAction : primaryActionTriggered ์ œ์–ด ์ด๋ฒคํŠธ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ๋™์ž‘. addAction(_:for:)์„ ํ†ตํ•ด ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋งŒ์•ฝ primaryAction ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ title๊ณผ image๋ฅผ ํฌํ•จํ•œ๋‹ค๋ฉด, ์ด ๋ฉ”์„œ๋“œ๋Š” ๊ทธ configuration์„ ๋ณต์‚ฌํ•ด ๋ฒ„ํŠผ ๋ชจ์–‘์— ์ ์šฉํ•œ๋‹ค.

 

 

 

 

 

 

 

2. Configuring titles

iOS 15 ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” title๊ณผ subTitle์„ ์ œ๊ณตํ•œ๋‹ค. Title๊ณผ SubTitle์€ ์•„๋ž˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํžˆ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

var title: String? { get set } ์ด ํ”„๋กœํผํ‹ฐ๋Š” attributedTitle ํ”„๋กœํผํ‹ฐ์˜ ๋ฌธ์ž์—ด ๊ฐ’๊ณผ ์ผ์น˜ํ•œ๋‹ค.
configurationUpdateHandler ๋˜๋Š” updateConfiguration().์„ ์‚ฌ์šฉํ•ด ๋ฒ„ํŠผ ์ƒํƒœ์— ๋”ฐ๋ผ title์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
var subtitle: String? { get set } ์ด ํ”„๋กœํผํ‹ฐ๋Š” attributedSubtitle ํ”„๋กœํผํ‹ฐ์˜ ๋ฌธ์ž์—ด ๊ฐ’๊ณผ ์ผ์น˜ํ•œ๋‹ค.
configurationUpdateHandler ๋˜๋Š” updateConfiguration().์„ ์‚ฌ์šฉํ•ด ๋ฒ„ํŠผ ์ƒํƒœ์— ๋”ฐ๋ผ title์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Title๊ณผ SubTitle์˜ ์Šคํƒ€์ผ์„ ์‚ฌ์šฉ์ž ์ง€์ •ํ•˜๋ ค๋ฉด AttributedString ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์•„๋ž˜ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋ฉด ๋œ๋‹ค.

var attributedTitle: AttributedString? { get set } configuration์€ ์ด ํ”„๋กœํผํ‹ฐ์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ title๊ณผ ์ผ์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
configurationUpdateHandler ๋˜๋Š” updateConfiguration().์„ ์‚ฌ์šฉํ•ด ๋ฒ„ํŠผ ์ƒํƒœ์— ๋”ฐ๋ผ title์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
var attributedSubtitle: AttributedString? { get set } configuration์€ ์ด ํ”„๋กœํผํ‹ฐ์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ subtitle๊ณผ ์ผ์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
configurationUpdateHandler ๋˜๋Š” updateConfiguration().์„ ์‚ฌ์šฉํ•ด ๋ฒ„ํŠผ ์ƒํƒœ์— ๋”ฐ๋ผ title์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

        var attributedTitle = AttributedString("ํƒ€์ดํ‹€")
        attributedTitle.font = .systemFont(ofSize: 25, weight: .bold)
        attributedTitle.foregroundColor = UIColor.black
        
        //๋‹จ๋ฝ ๊ฐ„๊ฒฉ ์กฐ์ •
        let paragraphTitle = NSMutableParagraphStyle()
        paragraphTitle.lineSpacing = 0.3 * (attributedTitle.font?.lineHeight ?? 1)
        paragraphTitle.lineBreakStrategy = .hangulWordPriority //์ค„ ๋ฐ”๊ฟˆ ์ „๋žต ์„ค์ •
        
        attributedTitle.paragraphStyle = paragraphTitle
        
        configuration.attributedTitle = attributedTitle

 

 

Title๊ณผ SubTitle์˜ ๊ฐ„๊ฒฉ๊ณผ alignment๋Š” ์•„๋ž˜์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

var titlePadding: CGFloat { get set } Title๊ณผ SubTitle ๊ฐ„๊ฒฉ ์กฐ์ •ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ
 var titleAlignment: UIButton.Configuration.TitleAlignment { get set } ๋ฒ„ํŠผ์— Title๊ณผ SubTitle์„ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœํผํ‹ฐ
case automatic, leading, trailing, center 

 

 

 

 

 


 

 

 

 

 

728x90