我讨厌Apple从Swift 3.1到Swift 4的所有变化。现在我又面临另一个困境。我能够自定义搜索栏,但现在我需要更改输入文本和占位符的来源。
我已经移动了天地,但我找不到改变字体的解决方案。我还想将搜索图标更改为自定义图标。
答案 0 :(得分:4)
Swift 4:
更改字体:
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0)]
更改颜色:
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = = [NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
搜索图标:
searchController.searchBar.setImage(UIImage(named: "search_icon"), for: UISearchBarIcon.search, state: .normal)
占位符:
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
答案 1 :(得分:1)
最新速度:
let attributes = [NSAttributedString.Key.font: UIFont.regular(15), NSAttributedString.Key.foregroundColor: UIColor.black]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = attributes
答案 2 :(得分:0)
在 Swift 5 中尝试此操作:
searchBar[keyPath: \.searchTextField].font = UIFont(...)