字体在定位Uibutton Xcode时会更改
当我尝试本地化Uibuttons时,字体会发生变化。本地化本身有效;我可以看到语言在变化,但是字体也在改变,我尝试使用基本国际化,本地化字符串和不同的豆荚进行本地化,并且字体仍在更改。我使用接口构建器创建了Uibuttons,并在接口构建器中设置了字体,还尝试通过编程方式创建UIBUTTON,但字体仍然更改。它只会改变Uibuttons,我没有Uilabels或Uitexfields的问题。
这就是Uibutton本地化之前的样子:
这就是Uibutton本地化后的样子:
这是我用来设置按钮并以编程方式进行本地化的代码:
@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:
This is what the UIButton looks like after localization:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
uibutton
可以通过两种主要方式配置对象:uibutton.configuration
当您在按钮上设置
Configuration
时,这些属性似乎并不清楚地记录下来。对于为iOS 15+创建的iOS项目,在属性Inspector中使用按钮配置设置的故事板中创建的按钮:普通,填充,灰色和有色的“样式”选项对应于静态plain()
,<代码>填充(),grey()
和tinted()
默认按钮配置当在情节提要中的
uibutton
上设置其中一种样式时,您尝试在代码中设置的属性被其配置所覆盖。您有两个主要选项:使用其配置来为目标的应用程序 iOS版本低于iOS 15,(1)是唯一的选择(无论如何,应该是故事板中的默认值);但是,如果您选择使用(2),则配置等效物看起来像:
UIButton
objects can be configured in two primary ways:UIButton.configuration
to set multiple properties all at onceAlthough 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 staticplain()
,filled()
,gray()
, andtinted()
default button configurations offered byUIButton.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: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: