Changing UINavigationBar font in Swift - ios

I have a UINavigationBar with a title in the middle. I have added a custom font ("Comic_Andy.ttf") to my app (I have checked info.plist to make sure it's listed, and I have checked the Copy Bundle Resources to make sure it has been added), and I would like the title of the UINavigationBar to be displayed in that font. From what I can gather it seems as though I'm supposed to use this in my ViewController:
myNavigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I placed that method in the viewDidLoad function of the ViewController. I have also tried this in the didFinishLaunchingWithOptions function of the AppDelegate:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I am programming in Swift, in XCode 6 Beta 6. Many resources regarding this task have mentioned using a method called setTitleTextAttributes, which is nowhere to be seen. I can't figure it out for the life of me - I've probably spent close to 3 hours on it by now - I have checked every StackOverflow answer, every website, so please do not mark this as a duplicate. Many thanks in advance!

Instead of using myNavigationBar, try navigationController.navigationBar in the ViewController viewDidLoad function. It worked for me.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
If this doesn't work, try using the .ttf suffix.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy.ttf", size: 22)]
Good luck!

Can also be done for all NavigationBars with UINavigationBar.appearance().
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: Font.mediumFontName, size: 20)!]

Well, it took me the whole day, but I got it. My first problem was I didn't even have a NavigationController, so once I got that all sorted out this is what finally fixed it. Turns out you call the font like this: Comic Andy instead of Comic_Andy. EVEN THOUGH the font is referred to throughout the entire package as Comic_Andy, and the file is named Comic_Andy, you still use Comic Andy.

Related

Actionsheet text is always grey (NSAttributedString.Key doesn't working)

I'm using a custom actionsheet with some text displaying. With XCode 11.3 I can't use NSAttributedString.Key.foreground. It used to work before the update, but now I can't find a solution.
Screengrab with example of my problem
let attributedMessageText = NSMutableAttributedString(
string: description,
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0),
NSAttributedString.Key.foregroundColor: UIColor.red
]
)
If I'm trying to change a background color, it turns gray too.
Maybe someone already faced this problem?
It's never a good idea to use private API as they might change without notice. I guess you use something like this:
actionSheet.setValue(message, forKey: "attributedTitle")
that's why this doesn't work on iOS13.3. Unfortunately there is no legal way to change those color with the public APIs. But you're not alone or here is good recommendation

UIBarButtonItem tintColor issue while setting bold text on in accessibility

UIBarButtonItem tintColor is not getting change after setting Bold Text On in Settings -> Display & Brightness -> Bold Text.
i am facing this issue in iOS 11 & 12 both. haven't checked in previous versions.
The same question is already asked in apple developer forum but i didn't find any answer there.
https://forums.developer.apple.com/thread/89337
if some one have any work around to this please suggest me.
You can try to do it programmatically using NSAttributedString, I have never done it trough the storyboard, but programmatically using array of [NSAttributedString.Key.font: .systemFontOfSize(size: 25), NSAttributedString.Key.foregroundColor: .blue], etc., works like a charm.
One method that works for me is changing the Global Tint option on the storyboard, of course this is not the perfect solution.

Custom Fonts Bug

TL;DR: Custom fonts couldn't be used programmatically before using them in a Storyboard/xib.
Note: I've checked out this, tried the answers and they didn't work. I've also made sure that they're in target membership.
I've noticed a strange bug while changing segment control title a custom font:
segmentedControl.titleTextAttributes = NSDictionary(objects: [UIFont.init(name: "Comfortaa-Regular",
size: UIFont.systemFontSize)!,
UIColor.white],
forKeys: [NSAttributedStringKey.font as NSCopying,
NSAttributedStringKey.foregroundColor as NSCopying]) as? [AnyHashable : Any]
It couldn't find the font, so unwrapping has failed. But the font could be seen in the Storyboard.
It's properly added to the project, here's the CopyBundle and InfoList:
So here's the catch; if I use it in the Storyboard, it is shown in the font families:
But if not, it's not shown -here I've changed to light and bold has disappeared-, and cannot be used programmatically.
I'm using Xcode Version 9.0 (9A235), and Swift 4. I've also checked OpenRadar and couldn't find a submission regarding this.
The mentioned font: Comfortaa
#EDUsta, I just tried with given font and its work ok, no issue in it, giving the step which i followed:
Add the Comfortaa-Bold.ttf, Comfortaa-Light.ttf, Comfortaa-Regular.ttf Font files to project.
2.Add the entries of Fonts in info.plist file
Make sure the font added in project target.
After it, apply the fonts on UILabel and UISegmentedControl using Swift code given below:
let fontFirst = UIFont(name: "Comfortaa-Bold", size: 16.0)!
segmentFont.setTitleTextAttributes([NSAttributedStringKey.font : fontFirst, NSAttributedStringKey.foregroundColor : UIColor.red],
for: .normal)
labelBold.font = UIFont(name: "Comfortaa-Bold", size: 16.0)
labelLight.font = UIFont(name: "Comfortaa-Light", size: 16.0)
labelRegular.font = UIFont(name: "Comfortaa-Regular", size: 16.0)
its work perfectly, you can check in below screenshot image:
For your reference i am adding the complete project here:
I have the same problem. I can't add exactly the same font family (Comfortaa) programatically - everytime it crashes, but once I add label in launchscreen and set font to Comfotaa-Bold, font loaded from code works fine and doesn't crash. So my solution is to add 3 labels with fonts such as - Comfortaa Bold, Comfortaa Regular, Comfortaa Light in launchscreen and set "hidden" flag on true. This way I'm able to use all of them programatically.

iOS 8: UIButton appearance font

Trying to set UIButton's font for all buttons using appearance in iOS 8. After Googling, it appears that Apple has changed the way you do this from OS to OS. In iOS 7, this should have worked:
UIButton.appearance().titleLabel?.font = UIFont.inviteButtonTitleFont()
But it doesn't seem to work anymore. Anyone know how you do it now?
Swift 3:
let fontRegular = UIFont(name: "MyCustomRegularFont", size: 17.0)
UILabel.appearance(whenContainedInInstancesOf: [UIButton.self]).font = fontRegular
UIButton.appearance().tintColor = UIColor.red
The proxy for font was removed from UILabel.appearance(), so that's the reason why this not works.
Use the appearance when contained in method on UILabel
UILabel.appearanceWhenContainedInInstancesOfClasses([UIButton.self]).font = UIFont.inviteButtonTitleFont()
This should work in iOS9, see this answer appearanceWhenContainedIn in Swift for iOS8 and 7 workarounds.
(Disclaimer, I have not compiled the code snippet so beware of typos).

WatchKit WKInterfaceLabel can't change font

I'm trying to change label font. But every font I set in the Attributes Inspector different from the system font - don't change anything - neither simulator or storyboard. I even tried to set the font programatically with Attributed string - the same System font appears. Thanks for your help.
You cannot use the included iOS fonts in WatchKit at this time. The only one available is System (San Francisco). Source: Apple Developer Forums
You can however use a custom font by adding the font file to the project:
Drag the font files into the project navigator
Include the custom font file in both your WatchKit app and WatchKit
extension bundle.
Add the Fonts provided by application (UIAppFonts) key to both your WatchKit app and your WatchKit Extension Info.plist files
Add this code to awakeWithContext to make sure you know the correct font name to call later in your code:
print("Custom font names:")
print(UIFont.fontNames(forFamilyName: "Exo"))
print(UIFont.fontNames(forFamilyName: "Tabardo"))
Run the app and take note of the font names printed to the debug console. Once you know the correct name, you can add this code somewhere in your WatchKit Extension:
var fontSize = CGFloat(32)
var text = "so cool"
var cstmFont = UIFont(name: "Tabardo", size: fontSize)!
var attrStr = NSAttributedString(string: text, attributes:
[NSFontAttributeName: cstmFont])
firstLabel.setAttributedText(attrStr)
fontSize = CGFloat(36)
text = "right on!"
cstmFont = UIFont(name: "Exo-Regular", size: fontSize)!
attrStr = NSAttributedString(string: text, attributes:
[NSFontAttributeName: cstmFont])
secondLabel.setAttributedText(attrStr)
Enjoy the custom fonts on the watch!
Keep in mind that glances and notifications cannot use custom fonts. If you want to use one there, you'll have to use a rendered image. However because glances and notifications should load quickly you'd want to have that image ready to go when it gets called.

Resources