I change the size and weight of the UINavigationBar font with the following:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
UINavigationBar.appearance().titleTextAttributes = [ NSAttributedStringKey.font: UIFont.systemFont(ofSize: 28, weight: .heavy)]
When I present the UIActivityViewController and select Mail, the subject line in the title (not the email) inherits the font change. In order to change this, I change the UINavigationBar back to something smaller.
While this works, and the subject line in the title is smaller, and now readable, when the UIActivityViewController returns, naturally the UINavigationBar font is still set to the new size and weight. I tried the completion as per below, but the font doesn't resize.
#objc func showActivityViewController() {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
UINavigationBar.appearance().titleTextAttributes = [ NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17, weight: .thin)]
present(activityViewController, animated: true, completion: {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
UINavigationBar.appearance().titleTextAttributes = [ NSAttributedStringKey.font: UIFont.systemFont(ofSize: 28, weight: .heavy)]
})
}
I change the font color of a UIButton in a similar way. I found that using the main thread helped. Try this and see if it works:
present(activityViewController, animated: true, completion: {
DispatchQueue.main.async {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 28, weight: .heavy)]
}
})
Related
I am trying to set the header title font to a much larger style font more like a big banner, and set a subtitle underneath it of the year.
extension FSCalendar {
func customizeCalendar() {
appearance.caseOptions = [.headerUsesUpperCase]
appearance.headerDateFormat = "MMM"
headerHeight = 100
let header = FSCalendarHeaderView()
header.largeContentTitle?.append("aldjsf")
appearance.headerTitleFont = UIFont(name: "SFProDisplay-Bold", size: 200)
appearance.headerTitleColor = COLOR_BLACK
appearance.headerTitleOffset = CGPoint(x: 0, y: 0)
appearance.headerMinimumDissolvedAlpha = 0.6
appearance.todayColor = COLOR_PRIMARY
appearance.todaySelectionColor = COLOR_BLACK
appearance.titleFont = UIFont(name: "SFProDisplay-Bold", size: 11)
appearance.titleSelectionColor = COLOR_BLACK
appearance.weekdayFont = UIFont(name: "SFProText-Semibold", size: 11)
appearance.weekdayTextColor = COLOR_GREY
appearance.eventDefaultColor = COLOR_BLACK
appearance.subtitleFont = UIFont(name: "SFProDisplay-Bold", size: 20)
appearance.selectionColor = COLOR_BLACK
}
}
Even though I am accessing the property .headerTitleFont it doesn't do anything ? I have tried all kinds of sizes. Any help appreciated, thank you.
Issue with the Font sizes
Seems the font you have mentioned is not available in the simulator/device thus it defaults to a font. I tried your approach with a font which is pre-installed and the headerTitle got changed as expected. List of pre-installed fonts
appearance.headerTitleFont = UIFont(name: "Noteworthy Light", size: 60)
This is how it appears with the above font
Adding a subtitle to the headerView
With the available API, it seems you cannot set a subtitle in the headerView. But alternatively you can achieve it like below by customizing the FSCalendarHeaderCell.titleLabel.attributedText. Note that below code only change the text of collectionView.visibleCells, so you will also have to execute this code when you scroll the headerView
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
for cell in calendar.calendarHeaderView.collectionView.visibleCells {
//create an attributedString with two lines and different font sizes
let attributedString = NSMutableAttributedString(string: "Sep\n2021\n")
let attributes0: [NSAttributedString.Key : Any] = [
.foregroundColor: UIColor.yellow,
.font: UIFont(name: "HelveticaNeue", size: 40)!
]
let attributes1: [NSAttributedString.Key : Any] = [
.foregroundColor: UIColor.systemGray2
]
attributedString.addAttributes(attributes0, range: NSRange(location: 0, length: 3))
attributedString.addAttributes(attributes1, range: NSRange(location: 4, length: 4))
//replace titleLabel attributedText with the one we created
(cell as! FSCalendarHeaderCell).titleLabel.attributedText = attributedString
}
}
This is how it appears after changing the attributedText of the FSCalendarHeaderCell.titleLabel
I'm trying to add two part of text with different font parameters.
But they always get value from first string.
And there one strange thing, font color is set independently.
let descriptionTextView: UITextView = {
let view = UITextView()
let attributedText = NSMutableAttributedString(string: "Text", attributes:
[.font:UIFont.boldSystemFont(ofSize: 20),
.foregroundColor: UIColor.blue])
attributedText.append(NSAttributedString(string: "\n\n\n Other text", attributes:
[.font: UIFont.italicSystemFont(ofSize: 5),
.foregroundColor: UIColor.gray]))
view.attributedText = attributedText
view.translatesAutoresizingMaskIntoConstraints = false
view.textAlignment = .center
view.font = UIFont.boldSystemFont(ofSize: 20)
view.isEditable = false
view.isScrollEnabled = false
return view
}()
This is how it look in simulator
Remove
view.font = UIFont.boldSystemFont(ofSize: 20)
I read a couple of stack overflow entries to change a searchBar's placeholder text-attributes. However, in iOS13, none of them really work.
I wonder how the font, font-size and font-color of a searchBar Placeholder can be changed under iOS13 ?
Here is what I tried:
let myAttributes = [NSAttributedString.Key.font: UIFont(name: "Avenir-Heavy", size: 28) as Any]
navigationItem.searchController?.searchBar.placeholder =
NSAttributedString(string: "placeholder text", attributes: myAttributes).string
Swift 5:
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
let atrString = NSAttributedString(string: "Search",
attributes: [.foregroundColor : color,
.font : UIFont.systemFont(ofSize: 10, weight: .bold)])
textfield.attributedPlaceholder = atrString
}
You can use this method without accessing the value for key since searchbar has searchTextField property. Add this code after initialising your searchbar.
let placeholder = NSAttributedString(string: "your placeholder text", attributes: [.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont(name: "Helvetica", size: 15)!])
searchBar.searchTextField.attributedPlaceholder = placeholder
I can adjust all the other aspects of the appearance of my navigation bar - but the font of 'Back' remains stubborn.
The MWE below shows four things I have tried to no avail
1)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 4)!], for: .normal)
return true
}
2) 3) 4)
class customNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedString.Key.font : UIFont(name: "Rockwell", size: 4)!,
NSAttributedString.Key.foregroundColor : UIColor.white,
], for: .normal )
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 7)!], for: .normal)
navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 4)!], for: .normal)
}
}
It's simplest in iOS 13:
let app = UINavigationBarAppearance()
app.backButtonAppearance.normal.titleTextAttributes = [
// whatever
]
UINavigationBar.appearance().standardAppearance = app
Before iOS 13, the API doesn't draw the distinction you want to draw. You just have to set the individual bar button item title text attributes for all your back buttons one at a time.
let title = // ...
let back = UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
back.setTitleTextAttributes([
// whatever
], for: .normal)
self.navigationItem.backBarButtonItem = back
(Remember also that your back bar button item is not the back bar button item when this view controller is visible, but when another view controller is pushed on top of this one.)
This solution seems to work well for me:
// Set all fonts in the navigation controller
class CustomNavigationController: UINavigationController {
// Font names
let normalFontName = "AppleSDGothicNeo-Medium"
let boldFontName = "AppleSDGothicNeo-Bold"
// Font size
let fontSize = CGFloat(13)
// Create fonts
let backButtonFont = UIFont(name: normalFontName, size: fontSize)
let titleFont = UIFont(name: boldFontName, size: fontSize)
override func viewDidLoad() {
super.viewDidLoad()
// Set standard appearance
let appearance = UINavigationBarAppearance()
appearance.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: backButtonFont]
appearance.titleTextAttributes = [NSAttributedString.Key.font: titleFont]
UINavigationBar.appearance().standardAppearance = appearance
}
}
I'm not seeing anything here on text kerning for the navigation bar. Anyone know how to do this? I have set up the text in ViewDidLoad()
self.navigationController?.navigationBar.topItem?.title = "My Nav Title"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Helvetica", size: 20)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
I see we can adjust kerning but can't get this code to work
attributes: [NSKernAttributeName: 5.0]
Thank Much!!!
I found and modified this code from an earlier post and it works now in xcode 7.3.1
let titleLabel = UILabel()
let attributes: NSDictionary = [
NSFontAttributeName:UIFont(name: "Helvetica", size: 20)!,
NSForegroundColorAttributeName:UIColor.blackColor(),
NSKernAttributeName:CGFloat(8.0)
]
let attributedTitle = NSAttributedString(string: ""My Nav Title", attributes: attributes as? [String : AnyObject])
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel