简体   繁体   中英

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:

在此处输入图像描述

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM