【问题标题】:Is is possible to use an imported custom font for a button in swift UI ios app?是否可以在 swiftUI ios 应用程序中为按钮使用导入自定义字体?
【发布时间】:2022-01-20 02:33:53
【问题描述】:

我是 swift 的初学者,并查看了 this site 以了解导入自定义字体的工作原理。我还查看了this StackOverflow question,这似乎没有解决我的具体问题

在查看网站后,我已将 .tts 文件 Lovelo-Black 添加到项目和 plist 中。

        private let loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("log in", for: .normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = Constants.cornerRadius
        button.backgroundColor = UIColorFromHex(rgbValue: 0x00FF9C,alpha: 1)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont(name: "Lovelo-Black") //issue here
        return button

是否可以让按钮使用 Lovelo-Black 字体?理想情况下,我希望它看起来像这样

如果可能的话,我应该用什么替换第 8 行?

【问题讨论】:

    标签: ios swift iphone xcode


    【解决方案1】:

    确保将字体正确添加到项目中。

    check this link

    使用正确的字体名称

    button.titleLabel?.font = UIFont(name: "Lovelo Black", size: size)

    【讨论】:

      【解决方案2】:

      在 swiftUI 中,您可以使用 Font 中的自定义函数来初始化和使用任何自定义字体。看看下面的示例代码。

      Font.custom("Lovelo_Black", size: 24)
      

      这会产生下面的结果

      【讨论】:

        猜你喜欢
        • 2012-05-01
        • 2016-04-11
        • 2011-02-12
        • 1970-01-01
        • 1970-01-01
        • 2019-11-05
        • 2012-02-18
        • 2017-03-29
        • 1970-01-01
        相关资源
        最近更新 更多