How can I programmatically change UIFont of UISearchBar or UITableViewHeader? - ios

I have a UITableView to which I add programmatically a UISearchController as a header. Since I'm adding it programmatically, I can't change the font from the storyboard. I need to change the UISearchBar font to match the font of the rest of the app. I would need to either change the font of a UISearchBar or of the whole tableViewHeader
I saw that there's a similar question with an answer in objective-c, however I'm developing my app in Swift and I tried to convert the answer on the other question to the following code but it still won't work :
let lightFont = UIFont(name: "GothamRounded-Light", size: 15)
for subview in searchController.searchBar.subviews {
if subview is UITextField{
(subview as! UITextField).font = lightFont
}
}

From this question
You'll need to loop through it's subviews to find the UITextField (not accessible by property)
for subview in search.subviews {
if subview is UITextField {
(subview as! UITextField).font = UIFont(name: "Arial", size: 24.0)
}
}
Then you can set it's font.

Try this:
let textFieldInSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInSearchBar?.font = UIFont.systemFont(ofSize: 10)

Add below code in viewDidLoad()
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
let placeholderLabel = textFieldInsideUISearchBar?.value(forKey: "placeholderLabel") as? UILabel
placeholderLabel?.font = UIFont(name: "Avenir Next", size: 13.0) //this will set custom font to placeholder font
textFieldInsideUISearchBar?.font = placeholderLabel?.font //this will set custom font to text font
This will set a custom font to both the placeholder and text in the UISearchBar.

if #available(iOS 9.0, *) {
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.blueColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UIFont.systemFontOfSize(17)
}

Related

Large navigation bar custom height

Is it possible to change large navigation bar height?
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.addSubview(expandedNavBar)
I dont think you can change navigationBar height because of Apple HIG. If you want to change navigationBar height , you can remove navigationBar and you can show your own customview to user like navigationBar
If you want to make the navigationBar a little bigger, you can try this trick by adding an empty string to the prompt property.
self.navigationItem.prompt = ""
Ok, I found this way:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.minimumLineHeight = 200
let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: paragraphStyle]
let navigationBar = navigationController?.navigationBar
if #available(iOS 13, *) {
if let appearance = navigationBar?.standardAppearance {
appearance.largeTitleTextAttributes = attributes
navigationBar?.standardAppearance = appearance
navigationBar?.scrollEdgeAppearance = appearance
}
} else {
navigationBar?.largeTitleTextAttributes = attributes
}

How to access UILabel without IBOutlets. Swift

I want to change the backgroundColor for several labels without using an outlet.
If i set tag for UILabel i can this
let label = self.darkView.viewWithTag(1) as? UILabel
label?.backgroundColor = UIColor.black
But I want to do this for multiple objects and without a tag. I tried this but it doesn't work
if let labels = self.darkView.subviews as? [UILabel] {
for label in labels {
label.backgroundColor = UIColor.black
}
}
How i can implemented this ?
You can iterate through all subViews of your custom view and check for each label like this:
for view in self.darkView.subviews as [UILabel] {
if let label = view as? UILabel {
label.backgroundColor = UIColor.black
}
}

How to stop a UITextField's attributed placeholder text from getting cut off on iOS 13?

I am creating a custom textfield for search bar purpose. I need custom font and font color. Though its working correctly on iOS 11-12.* but on iOS 13 its frame becomes small and thus the placeholder truncates. FYI I am using attributedPlaceholder for this. How can I stop this?
class SearchBar: UITextField {
#IBInspectable var placeholderText : String = "" {
didSet {
self.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.appFont(size: 14.0)])
}
}
After a lot of headache I figured out the solution.
Solution: I was adding leftView and rightView to maintain the padding. Instead of adding a view to the rightView of the textfield you need to add a button. Don't know why it works but it works. If someone has more to add you are welcome.
One solution would be to add an empty view inside your textField as its leftView property.
Wherever you set up your textField, say in your view controller in viewDidLoad, you can add the following:
//Create an empty padding view with width and height that you want
let emptyPaddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
//Add the view to your text field leftView property and set leftViewMode to .always
yourTextField.leftView = emptyPaddingView
yourTextField.leftViewMode = .always

How can i change the color of placeholder in SearchBar in Swift Xcode

I have this design of the search bar and the output is not the same.
I have tried TintColor, BackgroundColor, Different Styles in addition to many extensions on the internet and yet i still cannot achieve it.
All what i have so far is the normal searchbar with the grey area color at the placeholder.
To change the textColor for placeholder in SearchBar you need to extract the searchField from searchBar and then placeholderLabel from searchField. Here's how:
let searchField = searchBar.value(forKey: "searchField") as? UITextField
searchField?.textColor = myColor
let placeholderLabel = searchField?.value(forKey: "placeholderLabel") as? UILabel
placeholderLabel?.textColor = myColor

Swift 4 - Is there is way to set Avenir font for all button and labels present in Project or App delegate file

Is there is way to set Avenir font for all button and labels present in Project or App delegate file. I want a common place where i can set the font once so that it changes in all storyboards. how can i do this ?
Yes, it is possible.
For UILabel you can solve this problem with UIAppearance
UILabel.appearance().font = UIFont(name: "Avenir", size: 17)!
And for UIButton it's not as simple as with UILabel, but you can always use inheritance
class MyButton: UIButton {
override var titleLabel: UILabel? {
get {
let label = super.titleLabel
label?.font = UIFont(name: "Avenir", size: 17)!
return label
}
}
}

Resources