内容和失去焦点时的 UITextField 字体大小
UITextField font-size when content and losing focus
我有一个 UITextField,我想为其分配一个 自定义字体 和这样的字体大小
self.txtEmail.font = UIFont("HelveticaNeue-Light", size: 24)
当我启动应用程序时,一切看起来都很好。我的占位符文本使用新字体,如果我单击输入字段并开始输入,文本将使用新字体。但是,当我单击其他地方并且输入字段失去焦点时,字体将重置为默认字体。我发现更奇怪的是,我只为 UILabels 设置了这个默认字体,而不是 UITextField 通过在 AppDelegate 的应用程序方法中添加这一行.如果我尝试为 UITextField 设置默认字体,则没有任何反应。
UILabel.appearance().font = UIFont(name: "HelveticeNeue", size: 18)
所以我的问题是,为什么在 UITextField 获得内容并失去焦点之前,我会出现这样的行为:UITextField 具有正确的字体。
我有 1 条关于 UI 元素外观管理的一般性评论,还有一条关于 UITextField 的评论。
A) 外观:
文档说:
To customize the appearance of all instances of a class, use appearance to get the appearance proxy for the class.
事实上,这意味着您的更改将应用于进入您的应用程序 window 的每个 UILabel 实例(因为 iOS 应用程序只有 1 "public" window)。如果您想自定义包含在特定容器 class 中的标签的外观(例如在 UIView 中),您应该考虑使用
+ appearanceForTraitCollection:whenContainedIn:(不幸的是,自 iOS 9.0 以来它已被弃用)。一种更安全的方法是将继承用于您的目的,如下所示:MySpecialLabel -[inherits from and applies custom style attributes within its initializers and/or the awakeFromNib: method]-> UILabel
.
B) 为什么和UITextField有关?
那是因为 UITextField 在后台使用 UI 标签(如 _placeholderLabel
和 _promptLabel
)。
希望能帮到您解决问题。
我有一个 UITextField,我想为其分配一个 自定义字体 和这样的字体大小
self.txtEmail.font = UIFont("HelveticaNeue-Light", size: 24)
当我启动应用程序时,一切看起来都很好。我的占位符文本使用新字体,如果我单击输入字段并开始输入,文本将使用新字体。但是,当我单击其他地方并且输入字段失去焦点时,字体将重置为默认字体。我发现更奇怪的是,我只为 UILabels 设置了这个默认字体,而不是 UITextField 通过在 AppDelegate 的应用程序方法中添加这一行.如果我尝试为 UITextField 设置默认字体,则没有任何反应。
UILabel.appearance().font = UIFont(name: "HelveticeNeue", size: 18)
所以我的问题是,为什么在 UITextField 获得内容并失去焦点之前,我会出现这样的行为:UITextField 具有正确的字体。
我有 1 条关于 UI 元素外观管理的一般性评论,还有一条关于 UITextField 的评论。 A) 外观:
文档说:
To customize the appearance of all instances of a class, use appearance to get the appearance proxy for the class.
事实上,这意味着您的更改将应用于进入您的应用程序 window 的每个 UILabel 实例(因为 iOS 应用程序只有 1 "public" window)。如果您想自定义包含在特定容器 class 中的标签的外观(例如在 UIView 中),您应该考虑使用
+ appearanceForTraitCollection:whenContainedIn:(不幸的是,自 iOS 9.0 以来它已被弃用)。一种更安全的方法是将继承用于您的目的,如下所示:MySpecialLabel -[inherits from and applies custom style attributes within its initializers and/or the awakeFromNib: method]-> UILabel
.
B) 为什么和UITextField有关?
那是因为 UITextField 在后台使用 UI 标签(如 _placeholderLabel
和 _promptLabel
)。
希望能帮到您解决问题。