swift - how adjust kerning in navigation bar text? - swift

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

Related

How can you change the Header View title font on FSCalendar and add a Subtitle?

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

How to add strings with different fonts in UItextField?

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)

How to change Searchbar's Placeholder font and size in iOS13

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

Swift UIActivityViewController Email subject line inherits global UINavigationBar font change

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)]
}
})

Custom Navigation Title - SWIFT

I would like to customize my navigation title but run into a problem. "String is not identical to NSObject". Can someone point me in the right direction? My code is below,
let font = UIFont(name: "HelveticaNeue", size: 15.0)
let textFont = [NSFontAttributeName: font]
let navText = [NSAttributedString(string: "MY STRING HERE", attributes: textFont)]
var navString = UILabel()
navString.appendAttributedString(navText)
self.navigationItem.titleView = navString
UPDATE: I was able to solve the problem with the following code,
var navString: NSString = "MY STRING HERE"
var completedNavString = NSMutableAttributedString()
completedNavString = NSMutableAttributedString(string: navString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
var navLabel = UILabel()
navLabel.attributedText = completedNavString
navLabel.sizeToFit()
self.navigationItem.titleView = navLabel
var navString = UILabel()
navString.appendAttributedString(navText)
You can't "append" anything to a UILabel. Try navString.attributedText = navText.