iOS 8: UIButton appearance font - ios

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).

Related

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.

UINavigationBar background Image - broken in iOS 11?

The following code was working perfectly fine for setting a custom image as the background of a UINavigationBar:
// In AppDelegate.swift:
let image = UIImage(named:"HeaderBanner-new")
UINavigationBar.appearance().setBackgroundImage(image, for: UIBarMetrics.defaultPrompt)
Since switching to Xcode 9, Swift 4, and iOS 11, this code no longer works. All I'm getting is a plain white background in the UINavigationBar.
I also tried moving the code out of the AppDelegate.swift and putting it directly in my root custom UINavgiationController.swift file:
let image = UIImage(named:"HeaderBanner-new")
self.navigationBar.setBackgroundImage(image, for: UIBarMetrics.defaultPrompt)
Still doesn't work.
Any ideas what's going on - or ideas for a workaround/hack?
It seems NavigationBar.setBackgroundImage doesn't work as expected in iOS 11.
I had the same issue and fixed it by using NavigationBar.barTintColor and it worked.
Refer this answer.

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.

Compatibility of ios5 and ios6

I have a problem regarding incompatibility of ios5 vs ios6
While I was working on my project with ios5
label.textAlignment = UITextAlignmentCenter;
then updated to ios6
label.textAlignment = NSLineBreakByTruncatingTail;
Once I run my code on the device which ios5 installed, that give me error.
I am about to upload my app to appstore, however I want my app also supports ios5 as well. How I am going to handle both ios5 and ios6!
Thanks in advance!
The acceptable values for NSTextAlignment are:
NSTextAlignmentLeft
NSTextAlignmentCenter
NSTextAlignmentRight
NSTextAlignmentJustified
NSTextAlignmentNatural
Of those any of the first three will work equally under iOS 5 and 6.
NSLineBreakByTruncatingTail isn't really a valid type of text alignment though it'll look like NSTextAlignmentNatural at runtime because of the way the C enums line up.
So you'll need either to switch to NSTextAlignmentLeft across the board or to do a functionality check. Thankfully the change in text alignment type is in support of the new ability to push attributed strings to UILabel (and elsewhere) so that gives you something to hang off.
E.g.
if([label respondsToSelector:#selector(attributedText)])
label.textAlignment = NSTextAlignmentNatural;
else
label.textAlignment = NSTextAlignmentLeft;
Though the else is technically redundant because NSTextAlignmentLeft is the default value.

How do I use the new iOS 6 methods to center text?

My Old Code
sectorLabel.textAlignment = UITextAlignmentCenter;
Xcode is saying that textAlignment is deprecated. How do I use the new method?
Thank you
It now uses the same as the properties for the Mac.
Use NSTextAlignmentCenter instead :D
HTH

Resources