Swift設(shè)置圖片在Button的位置(上下左右及間距)

import UIKit

extension UIButton {

? ? /** 圖片在左,標(biāo)題在右 */

? ? func setIconInLeft() {

? ? ? ? setIconInLeftWithSpacing(0)

? ? }

? ? /** 圖片在右,標(biāo)題在左 */

? ? func setIconInRight() {

? ? ? ? setIconInRightWithSpacing(0)

? ? }

? ? /** 圖片在上,標(biāo)題在下 */

? ? func setIconInTop() {

? ? ? ? setIconInTopWithSpacing(0)

? ? }

? ? /** 圖片在下,標(biāo)題在上 */

? ? func setIconInBottom() {

? ? ? ? setIconInBottomWithSpacing(0)

? ? }

? ? //** 可以自定義圖片和標(biāo)題間的間隔 */

? ? func setIconInLeftWithSpacing(_ Spacing: CGFloat) {

? ? ? ? titleEdgeInsets = UIEdgeInsets()

? ? ? ? titleEdgeInsets.top = 0

? ? ? ? titleEdgeInsets.`left` = 0

? ? ? ? titleEdgeInsets.bottom = 0

? ? ? ? titleEdgeInsets.`right` = 0

? ? ? ? imageEdgeInsets = UIEdgeInsets()

? ? ? ? imageEdgeInsets.top = 0

? ? ? ? imageEdgeInsets.`left` = 0

? ? ? ? imageEdgeInsets.bottom = 0

? ? ? ? imageEdgeInsets.`right` = 0

? ? }

? ? func setIconInRightWithSpacing(_ Spacing: CGFloat) {

? ? ? ? let img_W: CGFloat? = imageView?.frame.size.width

? ? ? ? let tit_W: CGFloat? = titleLabel?.frame.size.width

? ? ? ? titleEdgeInsets = UIEdgeInsets()

? ? ? ? titleEdgeInsets.top = 0

? ? ? ? titleEdgeInsets.`left` = -(img_W + Spacing / 2)

? ? ? ? titleEdgeInsets.bottom = 0

? ? ? ? titleEdgeInsets.`right` = img_W + Spacing / 2

? ? ? ? imageEdgeInsets = UIEdgeInsets()

? ? ? ? imageEdgeInsets.top = 0

? ? ? ? imageEdgeInsets.`left` = tit_W + Spacing / 2

? ? ? ? imageEdgeInsets.bottom = 0

? ? ? ? imageEdgeInsets.`right` = -(tit_W + Spacing / 2)

? ? }

? ? func setIconInTopWithSpacing(_ Spacing: CGFloat) {

? ? ? ? let img_W: CGFloat? = imageView?.frame.size.width

? ? ? ? let img_H: CGFloat? = imageView?.frame.size.height

? ? ? ? let tit_W: CGFloat? = titleLabel?.frame.size.width

? ? ? ? let tit_H: CGFloat? = titleLabel?.frame.size.height

? ? ? ? titleEdgeInsets = UIEdgeInsets()

? ? ? ? titleEdgeInsets.top = tit_H / 2 + Spacing / 2

? ? ? ? titleEdgeInsets.`left` = -(img_W / 2)

? ? ? ? titleEdgeInsets.bottom = -(tit_H / 2 + Spacing / 2)

? ? ? ? titleEdgeInsets.`right` = img_W / 2

? ? ? ? imageEdgeInsets = UIEdgeInsets()

? ? ? ? imageEdgeInsets.top = -(img_H / 2 + Spacing / 2)

? ? ? ? imageEdgeInsets.`left` = tit_W / 2

? ? ? ? imageEdgeInsets.bottom = img_H / 2 + Spacing / 2

? ? ? ? imageEdgeInsets.`right` = -(tit_W / 2)

? ? }

? ? func setIconInBottomWithSpacing(_ Spacing: CGFloat) {

? ? ? ? let img_W: CGFloat? = imageView?.frame.size.width

? ? ? ? let img_H: CGFloat? = imageView?.frame.size.height

? ? ? ? let tit_W: CGFloat? = titleLabel?.frame.size.width

? ? ? ? let tit_H: CGFloat? = titleLabel?.frame.size.height

? ? ? ? titleEdgeInsets = UIEdgeInsets()

? ? ? ? titleEdgeInsets.top = -(tit_H / 2 + Spacing / 2)

? ? ? ? titleEdgeInsets.`left` = -(img_W / 2)

? ? ? ? titleEdgeInsets.bottom = tit_H / 2 + Spacing / 2

? ? ? ? titleEdgeInsets.`right` = img_W / 2

? ? ? ? imageEdgeInsets = UIEdgeInsets()

? ? ? ? imageEdgeInsets.top = img_H / 2 + Spacing / 2

? ? ? ? imageEdgeInsets.`left` = tit_W / 2

? ? ? ? imageEdgeInsets.bottom = -(img_H / 2 + Spacing / 2)

? ? ? ? imageEdgeInsets.`right` = -(tit_W / 2)

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • import UIKit ``` import SDWebImage class ViewController: ...
    nihau閱讀 535評(píng)論 0 1
  • 美團(tuán)iOS面試敗北感悟 | 掘金技術(shù)征文 http://www.cocoachina.com/special/20...
    小劉_假裝是個(gè)程序員閱讀 434評(píng)論 2 0
  • 項(xiàng)目中我們經(jīng)常會(huì)有這樣的需求,button中圖片左標(biāo)題右,圖片上標(biāo)圖下,圖片下標(biāo)題上,圖片右標(biāo)題左;感覺(jué)挺簡(jiǎn)單的是...
    coderJerry01閱讀 1,329評(píng)論 0 1
  • 導(dǎo)語(yǔ) 系統(tǒng)的 UIButton 默認(rèn)狀態(tài)下的樣式是圖標(biāo)在左標(biāo)題在右,但有時(shí)候可能需要不同的排版。當(dāng)然可以通過(guò)繼承添...
    iOSugarCom閱讀 3,252評(píng)論 1 2
  • 我爸爸有一副天生的大嗓門,不管他是心情平靜還是激動(dòng),分貝永遠(yuǎn)不會(huì)低于40。因?yàn)樗拇笊らT,媽媽沒(méi)少念叨他。 記得前...
    yhloop閱讀 1,031評(píng)論 5 7

友情鏈接更多精彩內(nèi)容