[英]Changing Search Bar placeholder text font in Swift
I am trying to change the font of the placeholder text in the search bar within my Search Display Controller.我正在尝试更改搜索显示控制器中搜索栏中占位符文本的字体。 I was looking at some examples and I tried to implement them but as they are in Objective-C, I wasn't able to find any that I could get to work.
我正在查看一些示例并尝试实现它们,但由于它们在 Objective-C 中,我无法找到任何可以开始工作的示例。
For example, I tried this one:例如,我试过这个:
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];
But I was unable to get past var textField: UITextField = UISearchBar
但我无法通过
var textField: UITextField = UISearchBar
Any ideas?有任何想法吗?
//SearchBar Text
let textFieldInsideUISearchBar = dashBoardSearchBar.valueForKey("searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.whiteColor()
//SearchBar Placeholder
let textFieldInsideUISearchBarLabel = textFieldInsideUISearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideUISearchBarLabel?.textColor = UIColor.whiteColor()
Set placeholder text font size:设置占位符文本字体大小:
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 12)
set search text font size:设置搜索文本字体大小:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 12)
This is the easiest practise for changing the Font or any other similar changes in the textfield of searchBar.这是更改搜索栏文本字段中的字体或任何其他类似更改的最简单做法。 I have been using XCode 8.4, Swift 3.x, iOS 10.x.
我一直在使用 XCode 8.4、Swift 3.x、iOS 10.x。
extension UISearchBar {
func change(textFont : UIFont?) {
for view : UIView in (self.subviews[0]).subviews {
if let textField = view as? UITextField {
textField.font = textFont
}
}
} }
The above code can be called directly where you make an IBOutlet of the searchBar...上面的代码可以在你制作searchBar的IBOutlet的地方直接调用...
@IBOutlet weak var searchBar: UISearchBar! {
didSet {
searchBar.change(textFont: GlobalConstants.Font.avenirBook14)
}
}
searchBar.searchTextField.font = UIFont(name: "Helvetica", size: 40)
In iOS 8 ,try this在 iOS 8 中,试试这个
for subView in searchBar.subviews {
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
textField.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Search", comment:""),
attributes:[NSForegroundColorAttributeName: UIColor.orangeColor()])
}
}
}
在Swift 5 中还有一种更简单的方法:
searchBar[keyPath: \.searchTextField].font = UIFont(...)
Swift 3 version of @Alvin's answer @Alvin 答案的 Swift 3 版本
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
let placeholderLabel = textFieldInsideUISearchBar?.value(forKey: "placeholderLabel") as? UILabel
placeholderLabel?.font = UIFont.systemFont(ofSize: 12.0)
This works perfectly for ios7 -> ios9 using swift 2:这适用于 ios7 -> ios9 使用 swift 2:
if #available(iOS 9.0, *) {
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UI.getInstance.tinyFont
} else {
func checkSubview(view:UIView)
for subView in view.subviews {
if subView is UITextField {
let textField = subView as! UITextField
textField.font = UI.getInstance.tinyFont
} else {
checkSubview(subView)
}
}
checkSubview(view)
}
Just replace UI.getInstance.tinyFont by whichever font you want.只需将 UI.getInstance.tinyFont 替换为您想要的任何字体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.