๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Application

[iOS/Swift] @IBInspectable, @IBDesignable ์‚ฌ์šฉํ•˜๊ธฐ - UIButton ์ปค์Šคํ…€ํ•˜๊ธฐ, UITextField ์ปค์Šคํ…€ํ•˜๊ธฐ

@IBInspectable, @IBDesignable ์•Œ์•„๋ณด๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ 
- UIButton ์ปค์Šคํ…€ํ•˜๊ธฐ, UITextField ์ปค์Šคํ…€ํ•˜๊ธฐ
- UIButton ํ…Œ๋‘๋ฆฌ ๋งŒ๋“ค๊ธฐ
- UITextField์— ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ

 


๐Ÿ’ก1. IB ?

@IBOutlet, @IBAction, @IBInspectable, @IBDesignable์™€ ๊ฐ™์ด IB๋กœ ์‹œ์ž‘ํ•˜๋Š” Attribute๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Xcode์—์„œ IB๋Š” Interface Builder๋ฅผ ์˜๋ฏธํ•˜๊ณ  ์Šคํ† ๋ฆฌ๋ณด๋“œ(Storyboard)๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, ์Šคํ† ๋ฆฌ๋ณด๋“œ์™€ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ฒฐ์‹œํ‚ฌ ๋•Œ, ์šฐ๋ฆฌ๋Š” IB๋กœ ์‹œ์ž‘ํ•˜๋Š” Attribute๋“ค์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด ์นœ๊ตฌ๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋žœ๋”๋ง ํ•ด๋ด…์‹œ๋‹ค.๐Ÿ˜Š


๐Ÿ’ก2. @IBInspectable ?

@IBInspectable์€ ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, Inspector + Able ์ธ์ŠคํŽ™ํ„ฐ์—์„œ ๊ฐ€๋Šฅํ•˜๋‹ค~๋ผ๋Š” ๋œป์œผ๋กœ

์ฝ”๋“œ์™€ ์Šคํ† ๋ฆฌ๋ณด๋“œ์˜ ์ธ์ŠคํŽ™ํ„ฐ ์˜์—ญ์„ ์ด์–ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด์˜ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š” ์†์„ฑ(Attribute)๋“ค์„ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ์ถ”๊ฐ€ํ•˜๊ณ  ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

UIButton๋ฅผ ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์›๋ž˜ ๋ฒ„ํŠผ์„ ์ปค์Šคํ…€ํ•  ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์ž–์•„์š”~

customBtn.layer.borderColor = UIColor.black.cgColor
customBtn.layer.borderWidth = 1
customBtn.layer.cornerRadius = 10

์ด์ œ ์ด ์ฝ”๋“œ ์ž‘์„ฑ ์—†์ด ์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ์ปค์Šคํ…€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. WOW~


๐Ÿ’ก3. @IBDesignable ?

์œ„ @IBInspectable์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ๋“ค์„ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋นŒ๋“œ ์ „๊นŒ์ง€ ์‹ค์ œ ๋ชจ์Šต ํ™•์ธ ๋ถˆ๊ฐ€!!!!!๐Ÿ˜ญ

๋”ฐ๋ผ์„œ, ์šฐ๋ฆฌ๋Š” @IBDesignable์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.๐Ÿ‘๐Ÿป

์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด(์ปดํฌ๋„ŒํŠธ)์™€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด์˜ ํ˜„์žฌ ์†์„ฑ์„ ์Šคํ† ๋ฆฌ๋ณด๋“œ์— ์‹ค์‹œ๊ฐ„ ๋ฐ˜์˜ํ•˜์—ฌ ๋ทฐ๋ฅผ ๊ทธ๋ ค์ค๋‹ˆ๋‹ค. @IBDesignable ํด๋ž˜์Šค๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ ๋„ ๋ทฐ๋ฅผ ์ •์˜ํ•œ ๋ชจ์Šต ๊ทธ๋Œ€๋กœ ์‹ค์‹œ๊ฐ„ ๋žœ๋”๋งํ•˜์—ฌ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@IBDesignable๋ฅผ ํด๋ž˜์Šค ์ƒ๋‹จ์— ์ ์Œ์œผ๋กœ์จ ์ด ํด๋ž˜์Šค๋Š” StoryBoard์— ์ƒํƒœ๋ฐ”๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ํด๋ž˜์Šค์ž„์„ Xcode์— ์•Œ๋ ค์ค๋‹ˆ๋‹ค.


๐Ÿ’ก HOW TO USE 1 ? -  1. UIButton ์ปค์Šคํ…€ํ•˜๊ธฐ

1) @IBInspectable, @IBDesignable ์†์„ฑ ์ž‘์„ฑ

๋จผ์ €, ๋ฒ„ํŠผ๊ณผ ์—ฐ๊ฒฐํ•  UIButton์„ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. 

ํด๋ž˜์Šค ์œ„์—๋Š” @IBDesignable ์„ ์–ธํ•˜์—ฌ ์‹ค์‹œ๊ฐ„ ๋žœ๋”๋ง์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

ํด๋ž˜์Šค ์•ˆ์—๋Š” ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ์†์„ฑ๋“ค์„ @IBInspectable ์„ ์ด์šฉํ•ด ์ธ์ŠคํŽ™ํ„ฐ ์˜์—ญ์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ Attribute ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค.

import UIKit

@IBDesignable
class MyInspector: UIButton {
    @IBInspectable var borderColor: UIColor{
        set {
            self.layer.borderColor = newValue.cgColor
        }
        get {
            let color = self.layer.borderColor ?? UIColor.clear.cgColor
            return UIColor(cgColor: color)
        }
    }
    
    @IBInspectable var borderWidth: CGFloat{
        set {
            self.layer.borderWidth = newValue
        }
        get {
            return self.layer.borderWidth
        }
    }
    
    @IBInspectable var cornerRadius: CGFloat{
        set {
            self.layer.cornerRadius = newValue
        }
        get {
            return self.layer.cornerRadius
        }
    }
}

2) ํด๋ž˜์Šค ์—ฐ๊ฒฐ

์•„์ด๋ดํ‹ฐํ‹ฐ ์ธ์ŠคํŽ™ํ„ฐ๋ทฐ์—์„œ ์ปค์Šคํ…€ํ•  ๋ฒ„ํŠผ๊ณผ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ค๋‹ˆ๋‹ค!!

3)lnspector Area ํ™•์ธ

์ง ~ ์—ฐ๊ฒฐ ํ›„ ์Šคํ† ๋ฆฌ๋ณด๋“œ์˜ ์ธ์ŠคํŒฉํ„ฐ ์˜์—ญ์„ ํ™•์ธํ•˜๋ฉด ์ฝ”๋“œ๋กœ ์ถ”๊ฐ€ํ•œ ์†์„ฑ๋“ค์ด ๋‚˜ํƒ€๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•œ๋ฒˆ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๋นŒ๋“œํ•˜๋ฉด, ์ž˜ ์ ์šฉ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!๐Ÿ‘๐Ÿป


๐Ÿ’ก HOW TO USE ? - 2. UITextField ์ปค์Šคํ…€ํ•˜๊ธฐ

1) @IBInspectable, @IBDesignable ์†์„ฑ ์ž‘์„ฑ

๋จผ์ € ์ฝ”์ฝ”์•„ ํ„ฐ์น˜ ํด๋ž˜์Šค๋กœ UITextField๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

ํด๋ž˜์Šค ์œ„์—๋Š” @IBDesignable ์„ ์–ธํ•˜์—ฌ ์‹ค์‹œ๊ฐ„ ๋žœ๋”๋ง์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

ํด๋ž˜์Šค ์•ˆ์—๋Š” ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ์†์„ฑ๋“ค์„ @IBInspectable ์„ ์ด์šฉํ•ด ์ธ์ŠคํŽ™ํ„ฐ ์˜์—ญ์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ Attribute ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ didSet์—์„œ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

@IBDesignable
class DesignableUITextField: UITextField {
    
    // Provides left padding for images
    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = super.leftViewRect(forBounds: bounds)
        textRect.origin.x += leftPadding
        return textRect
    }
    
    @IBInspectable var leftImage: UIImage? {
        didSet {
            updateView()
        }
    }
    
    @IBInspectable var leftPadding: CGFloat = 0
    
    @IBInspectable var color: UIColor = UIColor.lightGray {
        didSet {
            updateView()
        }
    }
    
    func updateView() {
        if let image = leftImage {
            leftViewMode = UITextField.ViewMode.always
            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
            imageView.contentMode = .scaleAspectFit
            imageView.image = image
            imageView.tintColor = color
            leftView = imageView
        } else {
            leftViewMode = UITextField.ViewMode.never
            leftView = nil
        }
        
        // Placeholder text color
        attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: color])
    }
}

 

2) ํด๋ž˜์Šค ์—ฐ๊ฒฐ

์•„์ด๋ดํ‹ฐํ‹ฐ ์ธ์ŠคํŽ™ํ„ฐ๋ทฐ์—์„œ ์ปค์Šคํ…€ํ•  ํ…์ŠคํŠธํ•„๋“œ์™€ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ค๋‹ˆ๋‹ค!!

3)lnspector Area ํ™•์ธ

์งœ์ž”!! ์ง ~ ์—ฐ๊ฒฐ ํ›„ ์Šคํ† ๋ฆฌ๋ณด๋“œ์˜ ์ธ์ŠคํŒฉํ„ฐ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ํ™•์ธํ•˜๋ฉด ์ถ”๊ฐ€ํ•œ ์†์„ฑ๋“ค์ด ๋‚˜ํƒ€๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•œ๋ฒˆ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๋นŒ๋“œํ•˜๋ฉด, ์ž˜ ์ ์šฉ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

728x90
๋ฐ˜์‘ํ˜•