๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[Swift] UIButton ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ with Configuration

by ํ‹ด๋”” 2022. 6. 28.
๋ฐ˜์‘ํ˜•

์ด๋ ‡๊ฒŒ ๊ทธ๋ฆผ๊ณผ ๊ธ€์ž๊ฐ€ ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋Š”๋ฐ

์ด๋•Œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์ˆ˜์ง ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค

์ถœ์ฒ˜

https://velog.io/@ezidayzi/iOS-UIButton-title-image-align-%ED%95%98%EA%B8%B0

 

[iOS] UIButton title , image vertical align ํ•˜๊ธฐ

UIButton๋ฅผ extesionํ•ด์„œ ์•ˆ์— ๋„ฃ์–ด๋‘๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค.๐Ÿ˜ณ

velog.io

extension UIButton {
    func alignTextBelow(spacing: CGFloat = 8.0) {
        
        
        guard let image = self.imageView?.image else {
            return
        }
        
        guard let titleLabel = self.titleLabel else {
            return
        }
        
        guard let titleText = titleLabel.text else {
            return
        }
        
        let titleSize = titleText.size(withAttributes: [
            NSAttributedString.Key.font: titleLabel.font as Any
        ])
        
        titleEdgeInsets = UIEdgeInsets(top: spacing, left: -image.size.width, bottom: -image.size.height, right: 0)
        imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0, bottom: 0, right: -titleSize.width)
        
        
    }
}

 

๋‹ค๋งŒ ๋ฌธ์ œ์ ์€...

deprecate!!!

Hig์—์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ์ด 15.0์—์„œ๋Š” UIButto.Configuration์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค

 

        if #available(iOS 15.0, *) {
            var configuration = UIButton.Configuration.plain()
            configuration.title = "๊ด€์‹ฌ ์ƒํ’ˆ"
            configuration.image = UIImage(named: "detail_bookmark_off")
            configuration.imagePadding = 2
            configuration.imagePlacement = .top
            configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
            button.configuration = configuration
        }

configuration์„ ์‚ฌ์šฉํ•˜๋ฉด ์•„์ฃผ ์‰ฝ๊ฒŒ ์ด๋ฏธ์ง€์™€ ํƒ€์ดํ‹€์„ ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค

configuration.imagePlacement๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด๋ฏธ์ง€์™€ ํƒ€์ดํ‹€ ๋ฐฐ์น˜๋ฅผ ์„ธํŒ…ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

์ด์ฐธ์— Configuration ๊ณต๋ถ€์ข€ ํ•ด๋‘ฌ์•ผ ๊ฒ ๋‹น๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

Polina Tankilevitch: https://www.pexels.com/ko-kr/photo/6419685/

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€