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

标签 ios swift xcode uibutton padding

我是一名初级 iOS 程序员,尝试开发一个 iOS 应用程序。我在应用程序的主页上放置了几个按钮,如图所示。

Front Page

请看红圈。图片和标题之间没有空格(填充/插入)。

实际上,我已经使用这段代码设置了图像和标题插图。但是,它并没有给出任何不同的结果。

    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)

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

编辑:(iOS 15+) 正如@SuryaKantSharma 评论的那样,这已被弃用。

配置按钮的新方法包括三个选项来控制填充和插入。

  • titlePadding:标题和副标题标签之间的距离。
  • imagePadding:按钮的图像和文本之间的距离
  • contentInsets:从按钮的内容区域到它的距离 界限。

有关 UIButton.Configuration 的示例和解释,请查看此链接:

这是 Sarun 撰写的三篇关于使用 UIButton (UIKit) 的文章中的第一篇。它们值得一读!

https://sarunw.com/posts/new-way-to-style-uibutton-in-ios15/

关于ios - 如何在 Swift 中为 UIButton 设置 Title Padding (Inset),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45589049/

相关文章:

swift - 通用数组上的函数

ios - 根据indexPath.section和indexPath.row判断多维结构的cell类型

ios - 数据持久化

swift - 覆盖在 swift 的扩展对象内部不起作用

iphone - -[NSFileManager removeItemAtPath :error:] makes app CRASH

ios - Xcode 8 中的 UITabbarController 在 Storyboard View 中显示了一个蓝色矩形

ios - UITableView 屏幕图像下载延迟加载

ios - 如何检查应用程序是否已安装或正在首次安装

ios - 在 NSArray 中更新 NSDictionary

ios - iTunes connect 无法创建新的 ios 应用程序版本