![](/img/trans.png)
[英]How to set unicode emoji to the the title of a UIButton in Swift 3?
[英]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)
這是因為標題邊緣插入(按設計)是在為文本調整大小之后插入的。
除了@mozahler 的回答,只有那個對我不起作用,必須添加 1 行以使其向左或向右對齊:
(它將標題與按鈕的左側對齊)
button.contentHorizontalAlignment =.left
button.titleEdgeInsets = UIEdgeInsets(上:0,左:10,下:5,右:5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.