簡體   English   中英

如何在 Swift 中為 UIButton 設置 Title Padding (Inset)

[英]How to set the Title Padding (Inset) for UIButton in Swift

我是初學者 iOS 程序員,嘗試開發一個 iOS 應用程序。 如圖所示,我在應用程序的主頁上放置了幾個按鈕。

首頁

請看紅色圓圈。 圖片和標題之間沒有空格(填充/插入)。

實際上,我已經使用這段代碼設置了圖像和標題插圖。 但是,它沒有給出任何不同的結果。

    buttonDoctor.titleLabel?.numberOfLines = 2
    buttonDoctor.titleLabel?.lineBreakMode = .byWordWrapping
    buttonMedical.titleLabel?.numberOfLines = 2
    buttonMedical.titleLabel?.lineBreakMode = .byWordWrapping
    buttonQuestion.titleLabel?.numberOfLines = 2
    buttonQuestion.titleLabel?.lineBreakMode = .byWordWrapping
    buttonLocation.titleLabel?.numberOfLines = 2
    buttonLocation.titleLabel?.lineBreakMode = .byWordWrapping
    buttonPromotion.titleLabel?.numberOfLines = 2
    buttonPromotion.titleLabel?.lineBreakMode = .byWordWrapping
    buttonDoctor.image(for: .normal)
    buttonMedical.image(for: .normal)
    buttonQuestion.image(for: .normal)
    buttonLocation.image(for: .normal)
    buttonPromotion.image(for: .normal)
    //buttonDoctor.titleLabel?.adjustsFontSizeToFitWidth = true
    //buttonPromotion.titleLabel?.adjustsFontSizeToFitWidth = true
    //buttonLocation.titleLabel?.adjustsFontSizeToFitWidth = true
    //buttonQuestion.titleLabel?.adjustsFontSizeToFitWidth = true
    //buttonMedical.titleLabel?.adjustsFontSizeToFitWidth = true



    let btnimg = try? UIImage(named: "doctorschedule.png")
    var newimg = imageResize(image: btnimg!!, scaledTo: CGSize(width: 36, height: 36))
    buttonDoctor.setImage(newimg, for: .normal)
    buttonDoctor.contentMode = .scaleAspectFit
    buttonDoctor.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
    buttonDoctor.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)

    let btnimg2 = try? UIImage(named: "medicalcheck.png")
    var newimg2 = imageResize(image: btnimg2!!, scaledTo: CGSize(width: 36, height: 36))
    buttonMedical.setImage(newimg2, for: .normal)
    buttonMedical.contentMode = .scaleAspectFit
    buttonMedical.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
    buttonMedical.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)

    let btnimg3 = try? UIImage(named: "survey.png")
    var newimg3 = imageResize(image: btnimg3!!, scaledTo: CGSize(width: 36, height: 36))
    buttonQuestion.setImage(newimg3, for: .normal)
    buttonQuestion.contentMode = .scaleAspectFit
    buttonQuestion.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
    buttonQuestion.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)

    let btnimg4 = try? UIImage(named: "location.png")
    var newimg4 = imageResize(image: btnimg4!!, scaledTo: CGSize(width: 36, height: 36))
    buttonLocation.setImage(newimg4, for: .normal)
    buttonLocation.contentMode = .scaleAspectFit
    buttonLocation.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
    buttonLocation.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)

    let btnimg5 = try? UIImage(named: "promotion.png")
    var newimg5 = imageResize(image: btnimg5!!, scaledTo: CGSize(width: 36, height: 36))
    buttonPromotion.setImage(newimg5, for: .normal)
    buttonPromotion.contentMode = .scaleAspectFit
    buttonPromotion.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
    buttonPromotion.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)

我已經為插圖嘗試了幾個值,但沒有給出不同的結果。 我的代碼有什么問題嗎?

請有人幫助我如何解決這個問題。 謝謝你

注意:我還有一個問題。 我想在首頁頂部放一個圖片標志(見橙色圓圈),但我不知道該怎么做。 由於沒有在導航欄中插入圖像的屬性。 僅供參考,它只是我從 storyboard(不是UINavigationController )放置的UIViewController內的常規導航欄。 如果您不介意,請就此提出一些建議。

注意:圓圈中的所有內容都是常規的 UIBUTTON,而不是 UIImage 和 UILabel 或 UIbutton。請參閱我的代碼。

對於程序化解決方案,您正在使用:

dismissButton.titleEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 0, right: 10)

什么時候應該使用:

someButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 0, right: 10)

這是因為標題邊緣插入(按設計)是在為文本調整大小之后插入的。

解決方案:作為查詢的解決方案,僅將標題的 left inset 設置為 10。 保持其他插入為 0 (或任何你想設置)

注意:

  • “內容插入”適用於標題和圖像。
  • “標題插入”僅適用於標題文本/字符串。
  • “圖像插入”僅適用於圖像/圖標。


試試這個(使用 Xcode 9+):

從情節提要(界面生成器)設計中,您可以使用 UIButton 的屬性設置插圖,如此快照所示。

在此處輸入圖片說明

將左右兩側的約束設置為 0,文本將居中。

在此處輸入圖片說明

在此處輸入圖片說明

除了@mozahler 的回答,只有那個對我不起作用,必須添加 1 行以使其向左或向右對齊:

(它將標題與按鈕的左側對齊)

button.contentHorizontalAlignment =.left

button.titleEdgeInsets = UIEdgeInsets(上:0,左:10,下:5,右:5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM