Search code examples
iosswiftxcodestoryboard

How to make UILabel and UIButton uppercased from storyboard


I can't find a way in the Storyboard to make my label or button text uppercased via a flag, please check my Storyboard screenshot where I show you the Attribute Inspector which has no flag for this kind:

enter image description here


Solution

  • Currently there is nothing ready for that, but you can add an attribute to the Interface Builder:

    Just add this to a file, call it for example XIB+Extension.swift:

    public protocol UIXIBStyle {
        var uppercased: Bool { get set }
    }
    
    extension UILabel: UIXIBStyle {
        @IBInspectable public var uppercased: Bool {
            get { return false }
            set(key) {
                if key {
                    text = text?.uppercased()
                }
            }
        }
    }
    
    extension UIButton: UIXIBStyle {
    
        @IBInspectable public var uppercased: Bool {
            get { return false }
            set(key) {
                if key {
                    setTitle(currentTitle?.uppercased(), for: .normal)
                }
            }
        }
    }
    

    And you will be able to see a new attribute called Uppercased under the Attribute Inspector for any UILabel or UIButton.