ios 在 Swift 中更改搜索栏占位符文本字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26441958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Search Bar placeholder text font in Swift
提问by user3746428
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?
有任何想法吗?
回答by A.G
//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()
回答by Mr. Bean
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)
}
}
回答by ldehai
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)
回答by Arshad
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()])
}
}
}
回答by Ajay Kumar
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)
回答by ramzesenok
There is even an easier way in Swift 5:
在Swift 5 中还有一种更简单的方法:
searchBar[keyPath: \.searchTextField].font = UIFont(...)
回答by ThomasE
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 替换为您想要的任何字体。
回答by Sourabh Kumbhar
searchBar.searchTextField.font = UIFont(name: "Helvetica", size: 40)