字体在定位Uibutton Xcode时会更改

发布于 2025-01-27 08:38:16 字数 894 浏览 2 评论 0原文

当我尝试本地化Uibuttons时,字体会发生变化。本地化本身有效;我可以看到语言在变化,但是字体也在改变,我尝试使用基本国际化,本地化字符串和不同的豆荚进行本地化,并且字体仍在更改。我使用接口构建器创建了Uibuttons,并在接口构建器中设置了字体,还尝试通过编程方式创建UIBUTTON,但字体仍然更改。它只会改变Uibuttons,我没有Uilabels或Uitexfields的问题。

这就是Uibutton本地化之前的样子:

button 1

这就是Uibutton本地化后的样子:

button 1

这是我用来设置按钮并以编程方式进行本地化的代码:

@IBOutlet weak var button1: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    button1.titleLabel?.font = UIFont(name: "Helvetica Bold", size: 20)
    
    button1.setAttributedTitle(NSAttributedString(string: NSLocalizedString("button1", comment: "")), for: .normal)

    
}

用于本地化使用接口构建器,我使用了一个POD,您可以在其中给UI元素一个局部密钥,但是出现了同样的问题。

The font changes when I’m trying to localize UIButtons. The localization itself works; I can see the language changing but the font is changing too I tried localizing using base internationalization, localized strings and different pods and the font still changes. I created the UIButtons using the interface builder and set the font in interface builder too, and also tried creating the UIButton programmatically but the font still changes. It only changes for UIButtons, I don’t have this problem with UILabels or UITexfields.

This is what the UIButton looks like before localization:

Button1

This is what the UIButton looks like after localization:

Button1

This is the code that I use to set the button and localize it programmatically:

@IBOutlet weak var button1: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    button1.titleLabel?.font = UIFont(name: "Helvetica Bold", size: 20)
    
    button1.setAttributedTitle(NSAttributedString(string: NSLocalizedString("button1", comment: "")), for: .normal)

    
}

For localization using the interface builder, I used a pod where you can give a UI element a localized key, but the same issue arises.

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

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

发布评论

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

评论(1

此刻的回忆 2025-02-03 08:38:16

uibutton可以通过两种主要方式配置对象:

  1. 传统上,通过按钮及其标签上的设置属性直接(如上所述中的代码中所做的),或
  2. 从iOS 15中进行设置,或通过 uibutton.configuration

当您在按钮上设置Configuration时,这些属性似乎并不清楚地记录下来。对于为iOS 15+创建的iOS项目,在属性Inspector中使用按钮配置设置的故事板中创建的按钮:普通,填充,灰色和有色的“样式”选项对应于静态plain(),<代码>填充(),grey()tinted()默认按钮配置

当在情节提要中的uibutton上设置其中一种样式时,您尝试在代码中设置的属性被其配置所覆盖。您有两个主要选项:

  1. 将按钮样式设置为默认故事板中的默认选项,避免将配置应用于按钮,并允许您使用传统属性来设置按钮样式,或
  2. 进行样式

使用其配置来为目标的应用程序 iOS版本低于iOS 15,(1)是唯一的选择(无论如何,应该是故事板中的默认值);但是,如果您选择使用(2),则配置等效物看起来像:

var attributes = AttributeContainer()
attributes.font = UIFont(name: "Helvetica Bold", size: 20)

let title = AttributedString(NSLocalizedString("button1", comment: ""), attributes: attributes)
button1.configuration.attributedTitle = title

UIButton objects can be configured in two primary ways:

  1. Traditionally, via setting attributes on the button and its labels directly (as you do in the code above), or
  2. As of iOS 15, via UIButton.configuration to set multiple properties all at once

Although this doesn't appear to be documented overly clearly, when you set a configuration on a button, those properties override anything which you might assign yourself to the button's properties. In the case of iOS projects created for iOS 15+, buttons created in storyboards default to being set up with a button configuration in the Attributes inspector: the Plain, Filled, Gray, and Tinted "style" options offered correspond to the static plain(), filled(), gray(), and tinted() default button configurations offered by UIButton.Configuration.

When one of these styles is set on the UIButton in the storyboard, the properties you attempt to set in code are overridden by its configuration. You have two main options:

  1. Set the button style to Default in the storyboard, which avoids applying a configuration to the button, and allowing you to use the traditional properties to style the button, or
  2. Style the button using its configuration instead

For apps which target iOS versions lower than iOS 15, (1) is the only option (and should be the default in storyboards anyway); but if you choose to go with (2), the configuration equivalent can look something like:

var attributes = AttributeContainer()
attributes.font = UIFont(name: "Helvetica Bold", size: 20)

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