更改 Swift 4 中 UISearchBar 的字体

Jua*_*hez 1 swift4 ios11

我讨厌苹果从 Swift 3.1 到 Swift 4 的所有变化。现在我又面临另一个困境。我能够自定义搜索栏,但现在我需要更改输入文本和占位符的来源。

在此输入图像描述

我已经感动了天地,但我找不到改变字体的解决方案。我还想将搜索图标更改为自定义图标。

小智 5

斯威夫特4:

更改字体:

UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0)]
Run Code Online (Sandbox Code Playgroud)

换颜色:

UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = = [NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
Run Code Online (Sandbox Code Playgroud)

搜索图标:

searchController.searchBar.setImage(UIImage(named: "search_icon"), for: UISearchBarIcon.search, state: .normal)
Run Code Online (Sandbox Code Playgroud)

占位符:

UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
Run Code Online (Sandbox Code Playgroud)