当按钮样式为“填充”时,如何将字体应用于按钮?

发布于 2025-01-13 14:31:14 字数 1461 浏览 5 评论 0原文

我想要实现的目标是一个带有

  • 填充框架的单选按钮,
  • 文本和图像之间的填充
  • 以及文本的特定字体。

我的问题是我无法成功组合所有 3 个条件。系统实现哪些预期属性取决于 IB 中特定按钮的样式属性设置。我也在程序代码中对填充和字体进行了更改,但经验表明它们被 IB 中的按钮属性所否决:

  • 当样式属性为“默认”时,可以成功应用所需的字体。 (无论是通过程序代码还是在 IB 中)。但这样填充就不起作用了。
  • 当样式属性为“填充”时,填充和框架参数将起作用,但不会应用字体。

这是一个简化的示例,我希望应用 Courier New 字体。我定义了两个单选按钮来演示 Style 属性的影响。首先是带有“默认”的版本。我们看到文本是 Courier。但没有框架和填充。此外,如果以编程方式设置填充,系统也会忽略它。

输入图片此处描述

接下来是带有“Filled”的版本。系统会忽略字体设置。这是我的问题: 输入图片这里的描述

有没有办法将框架、字体和填充结合起来?任何建议将不胜感激。作为参考,我也在其中进行了设置的代码段。我编写了一个 RadioButton 类,其中应应用图像、字体和填充。

class RadioButton: UIButton {
// Images
let checkedImage = UIImage(named: "radioButtonSet_25")! as UIImage
let uncheckedImage = UIImage(named: "radioButtonNotSet_25")! as UIImage

// Bool property
var isChecked: Bool = false {
    didSet {
        self.titleLabel?.font = UIFont(name: "CourierNewPSMT", size: 
CGFloat(15))
        self.configuration?.imagePadding = (50 as CGFloat)
        if isChecked == true {
            self.setImage(checkedImage, for: UIControl.State.normal)
        } else {
            self.setImage(uncheckedImage, for: UIControl.State.normal)
        }
    }
  }
}

The target I want to achieve is a radiobutton with

  • a filled frame,
  • a padding between text and image
  • and a specific font for the text.

My problem is that I cannot get all 3 conditions successfully combined. Which of the intended attributes are realized by the system depends on the setting of the Style attribute in the IB for the particular button. I have made the changes for padding and font in the program code also but experienced that they are overruled from the button attributes in the IB:

  • When the Style attribute is "Default", a required font can be applied successfully. (Regardless whether via program code or in the IB). But then padding does not work.
  • When the Style attribute is "Filled", paddings and frame parameters will work but the font is not applied.

Here a simplified example in which I want the Courier New font to be applied. I have two Radiobuttons defined to demonstrate the impact of the Style attribute. At first the version with "Default". We see that the text is Courier. But there is not frame and padding. Also if padding would be set programmatically, it is ignored by the system.

enter image description here

Next the version with "Filled". The font setting is ignored by the system. This is my problem:
enter image description here

Is there a way to get frame, font and padding be combined? Any advice would be appreciated. For reference here the piece of code in which I made the setting as well. I had written a class RadioButton in which the images, the font and the padding should be applied.

class RadioButton: UIButton {
// Images
let checkedImage = UIImage(named: "radioButtonSet_25")! as UIImage
let uncheckedImage = UIImage(named: "radioButtonNotSet_25")! as UIImage

// Bool property
var isChecked: Bool = false {
    didSet {
        self.titleLabel?.font = UIFont(name: "CourierNewPSMT", size: 
CGFloat(15))
        self.configuration?.imagePadding = (50 as CGFloat)
        if isChecked == true {
            self.setImage(checkedImage, for: UIControl.State.normal)
        } else {
            self.setImage(uncheckedImage, for: UIControl.State.normal)
        }
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2025-01-20 14:31:14

我认为您可能在 iOS 15 的故事板和新的基于配置的按钮中发现了一个错误。您必须在代码中设置字体。示例

    guard var config = button.configuration else {
        return
    }
    config.attributedTitle = try! AttributedString( NSAttributedString(string: "Button", attributes: [
        .font: UIFont(name: "Courier New Italic", size: 20)!,
        .foregroundColor: UIColor.systemOrange,
    ]), including: AttributeScopes.UIKitAttributes.self
                                            )
    button.configuration = config

结果如下:

在此处输入图像描述

仅供记录,这是我的故事板配置:

在此处输入图像描述

I think you've probably found a bug in the storyboard and the new configuration-based buttons of iOS 15. You'll have to set the font in code. Example

    guard var config = button.configuration else {
        return
    }
    config.attributedTitle = try! AttributedString( NSAttributedString(string: "Button", attributes: [
        .font: UIFont(name: "Courier New Italic", size: 20)!,
        .foregroundColor: UIColor.systemOrange,
    ]), including: AttributeScopes.UIKitAttributes.self
                                            )
    button.configuration = config

That results in this:

enter image description here

Just for the record, here's my storyboard configuration:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文