如何在Swift中为UIButton设置标题填充(插入)

chr*_*702 2 xcode padding uibutton ios 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)
Run Code Online (Sandbox Code Playgroud)

我已经为插图尝试了几个值,但它没有给出不同的结果.我的代码有什么问题吗?

请有人帮助我如何解决这个问题.谢谢

注意:我还有一个问题.我想在首页的顶部放一个图像标识(参见橙色圆圈),但我不知道该怎么做.由于没有在导航栏中插入图像的属性.仅供参考,它只是UIViewController我从故事板中放入的常规导航栏(不是UINavigationController).如果您不介意,请就此提出一些建议.

注意:圆圈中的所有内容都只是普通的UIBUTTON,而不是UIImage和UILabel或UI按钮.请看我的代码.

Moz*_*ler 13

对于程序化解决方案,您正在使用:

dismissButton.titleEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 0, right: 10)
Run Code Online (Sandbox Code Playgroud)

什么时候应该使用:

someButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 0, right: 10)
Run Code Online (Sandbox Code Playgroud)

这是因为标题边缘插入(按设计)是在为文本调整大小之后插入的。

  • .contentEdgeInsets 在 iOS 15 中被弃用 (2认同)

Kru*_*nal 6

解决方案: 作为查询的解决方案,将左侧插入设置为10仅用于标题.保持其他插图为0(或任何你想设置的)

注意:

  • "内容插入"适用于标题和图像.
  • '标题插入'仅适用于标题文本/字符串.
  • "图像插入"仅适用于图像/图标.


试试这个(使用Xcode 9+):

从storyboard(Interface Builder)设计中,您可以使用UIButton的属性设置插入,如此快照中所示.

在此输入图像描述

  • 在 iOS 15 中,请确保将“样式”也设置为“默认” (6认同)

abu*_*der 2

将左侧和右侧的约束设置为 0,文本将居中。

在此输入图像描述

在此输入图像描述