如何更改 UISearchBar 字体大小和颜色?
How to change UISearchBar Font Size & Color?
我 google 几个小时如何更改我的 UISearchBar 字体大小和颜色,但我找不到任何与之相关的文档。
这是我到目前为止在 swift 4:
上所做的
searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: view.frame.width - (menuImage.frame.width + iconImage.frame.width + 55), height: 30))
searchBar.placeholder = "SEARCH BAR"
searchBar.tintColor = UIColor.gray
searchBar.delegate = self
searchBar.font = [UIFont fontWithName:@"Oswald" size:11];
但它给了我一个错误。
你能告诉我如何更改 UISearchBar 的字体大小和颜色吗?
请帮忙!!!
要更改文本和占位符搜索栏:
// SearchBar text
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.red
textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(12)
// SearchBar placeholder
let labelInsideUISearchBar = textFieldInsideUISearchBar!.value(forKey: "placeholderLabel") as? UILabel
labelInsideUISearchBar?.textColor = UIColor.red
searchBarOutlet.setScopeBarButtonTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
您可以使用外观代理来设置 UISearchBar
文本和占位符的样式:
更改占位符:
let placeholderAppearance = UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self])
placeholderAppearance.font = UIFont(name: "Baskerville", size: 14)
placeholderAppearance.textColor = .red
更改搜索文本:
let searchTextAppearance = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
searchTextAppearance.font = UIFont(name: "Baskerville", size: 14)
searchTextAppearance.textColor = .green
这是更改字体和文本颜色的最新方法Swift:
let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(15), NSAttributedString.Key.foregroundColor: UIColor.gray]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = attributes
在 iOS 13 或最新版本中,UISearchBar 有一个名为 searchTextField
的 属性。但是更早的时候,我们不能用这个属性.
所以,我们可以扩展 UISearchBar:
extension UISearchBar {
var textField: UITextField? {
if #available(iOS 13.0, *) {
return self.searchTextField
} else {
// Fallback on earlier versions
for view in (self.subviews[0]).subviews {
if let textField = view as? UITextField {
return textField
}
}
}
return nil
}
}
并使用:
searchBar.textField?.font = UIFont.systemFont(ofSize: 12)
我 google 几个小时如何更改我的 UISearchBar 字体大小和颜色,但我找不到任何与之相关的文档。
这是我到目前为止在 swift 4:
上所做的 searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: view.frame.width - (menuImage.frame.width + iconImage.frame.width + 55), height: 30))
searchBar.placeholder = "SEARCH BAR"
searchBar.tintColor = UIColor.gray
searchBar.delegate = self
searchBar.font = [UIFont fontWithName:@"Oswald" size:11];
但它给了我一个错误。
你能告诉我如何更改 UISearchBar 的字体大小和颜色吗?
请帮忙!!!
要更改文本和占位符搜索栏:
// SearchBar text
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.red
textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(12)
// SearchBar placeholder
let labelInsideUISearchBar = textFieldInsideUISearchBar!.value(forKey: "placeholderLabel") as? UILabel
labelInsideUISearchBar?.textColor = UIColor.red
searchBarOutlet.setScopeBarButtonTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
您可以使用外观代理来设置 UISearchBar
文本和占位符的样式:
更改占位符:
let placeholderAppearance = UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self])
placeholderAppearance.font = UIFont(name: "Baskerville", size: 14)
placeholderAppearance.textColor = .red
更改搜索文本:
let searchTextAppearance = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
searchTextAppearance.font = UIFont(name: "Baskerville", size: 14)
searchTextAppearance.textColor = .green
这是更改字体和文本颜色的最新方法Swift:
let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(15), NSAttributedString.Key.foregroundColor: UIColor.gray]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = attributes
在 iOS 13 或最新版本中,UISearchBar 有一个名为 searchTextField
的 属性。但是更早的时候,我们不能用这个属性.
所以,我们可以扩展 UISearchBar:
extension UISearchBar {
var textField: UITextField? {
if #available(iOS 13.0, *) {
return self.searchTextField
} else {
// Fallback on earlier versions
for view in (self.subviews[0]).subviews {
if let textField = view as? UITextField {
return textField
}
}
}
return nil
}
}
并使用:
searchBar.textField?.font = UIFont.systemFont(ofSize: 12)