简体   繁体   English

如何在 Swift 中为 UIButton 设置 Title Padding (Inset)

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

I'm a beginner iOS programmer and try to develop an iOS application.我是初学者 iOS 程序员,尝试开发一个 iOS 应用程序。 I have several buttons that I put on the home page of the app like in the picture.如图所示,我在应用程序的主页上放置了几个按钮。

首页

please look at the red circle.请看红色圆圈。 There's no space (padding/insets) between the image and the title.图片和标题之间没有空格(填充/插入)。

actually, I already set the image and title insets using this code.实际上,我已经使用这段代码设置了图像和标题插图。 But, it did not give any different result.但是,它没有给出任何不同的结果。

    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)

I already tried several value for the insets, but it give no different result.我已经为插图尝试了几个值,但没有给出不同的结果。 did I do anything wrong with my code?我的代码有什么问题吗?

please some one kindly help me how to solve this problem.请有人帮助我如何解决这个问题。 thank you谢谢你

Note: I have one more concern.注意:我还有一个问题。 I want to put an image logo at the top of the front page (see the orange circle), but I don't know how to do it.我想在首页顶部放一个图片标志(见橙色圆圈),但我不知道该怎么做。 Since there's no property to insert an image in the navigation bar.由于没有在导航栏中插入图像的属性。 FYI, it's just regular navigation bar inside the UIViewController that I put from the storyboard (not UINavigationController ).仅供参考,它只是我从 storyboard(不是UINavigationController )放置的UIViewController内的常规导航栏。 If you don't mind, please give some advice about this.如果您不介意,请就此提出一些建议。

NOTE: Everything in the circle is just a regular UIBUTTON not UIImage and UILabel or UIbutton. Please see my code.注意:圆圈中的所有内容都是常规的 UIBUTTON,而不是 UIImage 和 UILabel 或 UIbutton。请参阅我的代码。

For a programmatic solution, you are using:对于程序化解决方案,您正在使用:

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

when you should be using:什么时候应该使用:

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

This is because title edge insets (by design) inset AFTER it has been sized for the text.这是因为标题边缘插入(按设计)是在为文本调整大小之后插入的。

Solution: As a solution to your query, set left inset to 10 for title only.解决方案:作为查询的解决方案,仅将标题的 left inset 设置为 10。 Keep other inset as 0 (or whatever you want to set)保持其他插入为 0 (或任何你想设置)

Note:注意:

  • 'Content inset' is applicable on both title and image. “内容插入”适用于标题和图像。
  • 'Title inset' is applicable on title text/string only. “标题插入”仅适用于标题文本/字符串。
  • 'Image inset' is applicable on image/icon only. “图像插入”仅适用于图像/图标。


Try this (with Xcode 9+):试试这个(使用 Xcode 9+):

From storyboard (Interface Builder) design, you can set inset using properties of UIButton, as shown in this snapshot.从情节提要(界面生成器)设计中,您可以使用 UIButton 的属性设置插图,如此快照所示。

在此处输入图片说明

Set the constraints left and right side to 0, the text would then centered.将左右两侧的约束设置为 0,文本将居中。

在此处输入图片说明

在此处输入图片说明

In addition to @mozahler answer, only that didn't work for me, have to add 1 line to make it align to left or right:除了@mozahler 的回答,只有那个对我不起作用,必须添加 1 行以使其向左或向右对齐:

(It will align title to left side of button) (它将标题与按钮的左侧对齐)

button.contentHorizontalAlignment =.left button.contentHorizontalAlignment =.left

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM