iOS15 UIButton configuration

直接上代碼,看注釋

let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(filterConditionSelectAction(btn:)), for: .touchUpInside)
if #available(iOS 15.0, *) {
    // 初始化一個(gè)configuration姜胖,有多種方法誉帅,可根據(jù)需要選擇
    btn.configuration = UIButton.Configuration.plain()
    // title 和 subtitle的對(duì)其關(guān)系,文本是上下排版的
    btn.configuration?.titleAlignment = .leading
    // title he subtitle的間距
    btn.configuration?.titlePadding = 16.scaleToScreen()
    // image 和 文本 的相對(duì)位置
    btn.configuration?.imagePlacement = .trailing
    // image 和 文本的間距
    btn.configuration?.imagePadding = 16.scaleToScreen()
    // button的內(nèi)容(title谭期,subtitle堵第,image)顯示后與按鈕的邊距,默認(rèn)由一段距離
    btn.configuration?.contentInsets = NSDirectionalEdgeInsets.zero
    // 設(shè)置按鈕的狀態(tài)變化的監(jiān)聽(tīng)隧出,根據(jù)變化來(lái)改變按鈕的顯示內(nèi)容,之前都是全部設(shè)置進(jìn)去自動(dòng)切換的阀捅,現(xiàn)在不行了
    btn.configurationUpdateHandler = {(button: UIButton) -> Void in
        //根據(jù)狀態(tài)修改內(nèi)容
        switch button.state {
        case .normal, .highlighted:
            // image變化
            button.configuration?.image = UIImage.arrowPickUp
            // 背景的變化胀瞪,默認(rèn)會(huì)有些自帶效果,
            button.configuration?.background.backgroundColor = .clear
            // 字體的樣式與之前一樣
            button.configuration?.attributedTitle = AttributedString(title, attributes: AttributeContainer([
                NSAttributedString.Key.font : UIFont.customMediumFont(ofSize: UIFont.sizeL),
                NSAttributedString.Key.foregroundColor : UIColor.whiteAlpha50 ?? .red]))
            // 子標(biāo)題饲鄙,iOS15之后才有凄诞,對(duì)應(yīng)的button.subtitleLabel
            button.configuration?.subtitle = "abc"

        case .selected, [.selected, .highlighted]:
            // image變化,與上面對(duì)應(yīng)
            button.configuration?.image = UIImage.arrowUnfold

        case .disabled:
            Log.Debug(message: "不可\(btn.state)")
        default:
            Log.Debug(message: "默認(rèn)值\(btn.state)")
        }
        button.updateConfiguration()
        
    }
} else {
    // iOS15 之前的屬性設(shè)置
    btn.setTitle(title, for: .normal)
    // 字體顏色
    btn.setTitleColor(UIColor.whiteAlpha50, for: .normal)
    btn.setTitleColor(UIColor.whiteAlpha50, for: .selected)
    // 設(shè)置圖片各種狀態(tài)都需要設(shè)置
    btn.setImage(UIImage.arrowUnfold, for: .normal)
    btn.setImage(UIImage.arrowUnfold, for: .highlighted)
    btn.setImage(UIImage.arrowPickUp, for: .selected)
    btn.setImage(UIImage.arrowPickUp, for: [.selected, .highlighted])
    btn.titleLabel?.font = UIFont.customFont(ofSize: UIFont.sizeL)
    // 改變title與image的位置關(guān)系
    btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -15.scaleToScreen(), bottom: 0, right: 15.scaleToScreen())
    btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 51.scaleToScreen(), bottom: 0, right: -51.scaleToScreen())
    btn.backgroundColor = .gray
}
view.addSubview(btn)

按鈕的內(nèi)部布局控制比之前簡(jiǎn)單很多忍级,不像之前帆谍,簡(jiǎn)單的 可以通過(guò)titleEdgeInsets 和 imageEdgeInsets來(lái)處理,復(fù)雜的就需要自定了轴咱,

自定義demo

import UIKit

public enum CustomButtonLayoutType : Int {
    case system
    case imageTop
    case imageRight
    case avatar
}

public class CustomButton: UIButton {
    
    public var cusFont: UIFont? {
        get {
            return self.titleLabel?.font
        }
        set {
            self.titleLabel?.font = newValue
        }
    }
    
    public var layoutType: CustomButtonLayoutType = .system
    
    override public func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        
        if layoutType == .system || contentRect == CGRect.zero {
            return super.titleRect(forContentRect: contentRect)
        }
        let imageSize = super.imageRect(forContentRect: contentRect)
        let titleSize = calculateTitleSize(size: contentRect.size)
        
        switch layoutType {
        case .imageTop:
            let x = (contentRect.width - titleSize.width) / 2 + contentRect.minX + self.titleEdgeInsets.left
            let y = (contentRect.height + imageSize.height - titleSize.height) / 2 + contentRect.minY + self.titleEdgeInsets.top
            return CGRect(x: x, y: y, width: titleSize.width, height: titleSize.height)
            
        case .imageRight:
            let x = (contentRect.width - imageSize.width - titleSize.width) / 2 + contentRect.minX + self.titleEdgeInsets.left
            let y = (contentRect.height - titleSize.height) / 2 + contentRect.minY + self.titleEdgeInsets.top
            return CGRect(x: x, y: y, width: titleSize.width, height: titleSize.height)
            
        default:
            return super.titleRect(forContentRect: contentRect)
        }
    }
    
    override public func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        if layoutType == .system || contentRect == CGRect.zero{
            return super.imageRect(forContentRect: contentRect)
        }
        
        let imageSize = super.imageRect(forContentRect: contentRect)
        let titleSize = calculateTitleSize(size: contentRect.size)
        
        switch layoutType {
        case .imageTop:
            let x = (contentRect.width - imageSize.width) / 2 + contentRect.minX + self.imageEdgeInsets.left
            let y = (contentRect.height - imageSize.height - titleSize.height) / 2 + contentRect.minY + self.imageEdgeInsets.top
            return CGRect(x: x, y: y, width: imageSize.width, height: imageSize.height)
            
        case .imageRight:
            let x = (contentRect.width + titleSize.width - imageSize.width) / 2 + contentRect.minX + self.imageEdgeInsets.left
            let y = (contentRect.height - imageSize.height) / 2 + contentRect.minY + self.imageEdgeInsets.top
            return CGRect(x: x, y: y, width: imageSize.width, height: imageSize.height)
            
        case .avatar:
            let bgImageWidth: CGFloat = self.currentBackgroundImage?.size.width ?? imageSize.width
            let width: CGFloat = imageSize.width * contentRect.width / bgImageWidth
            let height = width * imageSize.height / imageSize.width
            let x = (contentRect.width - width) + contentRect.minX + self.imageEdgeInsets.left
            let y = (contentRect.height - height) + contentRect.minY + self.imageEdgeInsets.top
            return CGRect(x: x, y: y, width: width, height: height)
        default:
            return super.imageRect(forContentRect: contentRect)
        }
    }
    
    override public func backgroundRect(forBounds bounds: CGRect) -> CGRect {
        return super.backgroundRect(forBounds: bounds)
    }
    
    private func calculateTitleSize(size: CGSize) -> CGSize {
        if let title = self.currentAttributedTitle {
            return title.size()
        }
        if let title = self.currentTitle {
            let options: NSStringDrawingOptions = .usesLineFragmentOrigin
            let attributes : [NSAttributedString.Key : Any] = [.font: cusFont ?? UIFont.customFont(ofSize: UIFont.size1xl)]
            return title.boundingRect(with: size, options: options, attributes: attributes, context: nil).size
        }
        return CGSize.zero
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末汛蝙,一起剝皮案震驚了整個(gè)濱河市烈涮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌窖剑,老刑警劉巖坚洽,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異西土,居然都是意外死亡讶舰,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)需了,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)跳昼,“玉大人,你說(shuō)我怎么就攤上這事肋乍《旒眨” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵住拭,是天一觀的道長(zhǎng)挪略。 經(jīng)常有香客問(wèn)我,道長(zhǎng)滔岳,這世上最難降的妖魔是什么杠娱? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮谱煤,結(jié)果婚禮上摊求,老公的妹妹穿的比我還像新娘。我一直安慰自己刘离,他們只是感情好室叉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著硫惕,像睡著了一般茧痕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恼除,一...
    開(kāi)封第一講書(shū)人閱讀 51,287評(píng)論 1 301
  • 那天踪旷,我揣著相機(jī)與錄音,去河邊找鬼豁辉。 笑死令野,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的徽级。 我是一名探鬼主播气破,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼餐抢!你這毒婦竟也來(lái)了现使?” 一聲冷哼從身側(cè)響起低匙,我...
    開(kāi)封第一講書(shū)人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎朴下,沒(méi)想到半個(gè)月后努咐,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡殴胧,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年渗稍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片团滥。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡竿屹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出灸姊,到底是詐尸還是另有隱情拱燃,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布力惯,位于F島的核電站碗誉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏父晶。R本人自食惡果不足惜哮缺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望甲喝。 院中可真熱鬧尝苇,春花似錦、人聲如沸埠胖。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)直撤。三九已至非竿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間谋竖,已是汗流浹背汽馋。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留圈盔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓悄雅,卻偏偏與公主長(zhǎng)得像驱敲,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宽闲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354