ios 7 UIBarButtonItem UIAppearance not setting font - ios7

I'm using this piece of code to set the default font (Custom) for all my UIBarButtonItems:
NSDictionary *attributesBarButtonItem = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"ProximaNova-Light" size:18.0], NSFontAttributeName, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributesBarButtonItem forState:UIControlStateNormal];
NSLog(#"%#", [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]);
However, it seems to be ignored as the font does not change, and NSLog returns (null). It's a bit confusing because its pretty much the same code I'm using to set the default font for all my navigation bars and it works fine for them.
The piece of code is placed in AppDelegate´s didFinishLaunchingWithOptions but I've also test it in other viewControllers (viewDidLoad) with exact same result.
Other strange behaviour I've noticed:
I've got a tab bar controller, and when I load any viewController with bar button items it doesn't work, but if I push another viewController it works (The font is changed to the selected one), and it keeps working even if that viewController is popped out, although it will stop working if another tab is pushed.
Any help to try to set a default font for the UIBarButtonItems would be appreciated. Thanks!

Is this your custom font ?
There could be few problems:
is the font in TTF format ?
if you click on the font in xcode is Target membership in right panel checked ?
did you add the font to project plist file ?
Also you should use UITextAttributeFont in the dictionary:
[UIBarButtonItem appearance] setTitleTextAttributes:#{UITextAttributeFont:[UIFont fontWithName:#"ProximaNova-Light" size:18.0]} [forState:forState:UIControlStateNormal];

I had a similar issue arise because I was creating the leftBarButtonItem before I set the appearance attributes. Swapping the order such that appearance was set first fixed the problem.

Related

leftBarButtonItem setTitleTextAttributes issue in iOS11

I have an app with a deployment target of iOS 9.3.
I have just upgraded to Xcode 9.0.1, and have noticed this issue across all simulator devices and my own iPhone7 device running iOS11. The issue does not impact devices running < iOS11.
I am initialising a left bar button item, with a custom font as follows (in viewDidLoad):
UIBarButtonItem *safeModeButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:#selector(toggleSafeMode)];
[safeModeButton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Sosa-Regular" size:31],NSFontAttributeName,
nil]forState:UIControlStateNormal];
[self.navigationItem setLeftBarButtonItem:safeModeButton];
self.navigationItem.leftItemsSupplementBackButton = YES;
Shortly after in another method, I set the bar button title as follows:
self.navigationItem.leftBarButtonItem.title = #"è";
The problem is, I am seeing the actual è text on the button, rather then the symbol which should be rendered. è for the "Sosa-Regular" font is a symbol.
I previously didn't have this problem prior to the Xcode9/iOS11 upgrade. I have tried explicitly setting the titleTextAttributes before I set the title, but it always just shows the è. It's as if the titleTextAttributes is not persistent or setting the title outside viewDidLoad resets the titleTextAttributes for the button. If I set the title text in viewDidLoad, it's all working ok.
Any ideas would be appreciated.
Found the answer to this after playing around for a while. Shortly after initialising the UIBarButtonItem, I set it to enabled = false.
As I only specified the title text attributes for UIControlStateNormal, it was not applicable for UIControlStateDisabled. Strange this only came up with iOS11. So adding this line fixed the problem:
[safeModeButton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Sosa-Regular" size:31],NSFontAttributeName,
nil]forState:UIControlStateDisabled];

How to change color of MFMailComposeViewController texts and button images?

iOS uses standard (blue) tint color for all colored texts in MFMailComposeViewController. This is not good for me, as customer wants his company colors in app. How to change their color to orange?
I am asking specifically about colors of button icons (add image and bell image) and texts containing mail addresses. I already have navigation bar colors changed. In documentation, there is written:
The view hierarchy of this class is private and you must not modify it. You can, however, customize the appearance of an instance by using the UIAppearance protocol.
I have tried to use it, but it is not working (I might doing it a wrong way, as I do not know UIAppearance). This is what I have tried:
[[UIButton appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[[UILabel appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setTextColor:[UIColor orangeColor]];
As Apple says: https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMailComposeViewController_class/index.html
The view hierarchy of this class is private and you must not modify
it. You can, however, customize the appearance of an instance by using
the UIAppearance protocol.
Anyway, you can check this post:
Customizing automatic MFMailComposeViewController opened from UITextView
I got this problem too. Just use:
UIView.appearance().tintColor = .orange
This works fine but there is a flaw. The recipients text will change back to system tintColor(blue) when editing.

Changing Font in UITextView does not work in iOS7

I have a UITextView with text in it, the View is in a UITableViewCell. I noticed that the font was not quite the same on iOS7 as with iOS6, noting it was set to "system" I decided to specify the exact font/size.
It appeared nothing happened so I thought I would do a better test (big font not used anywhere), like this in my "CellForRowAt....";
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
In iOS6 it comes out like this;
In iOS7 it comes out like this;
It happens in just a few places in the app but it is very annoying, can't figure out why? I am fast getting to the point where I may use the iOS7 Font/ Size throughout the app.
Some extra info;
The UITextView is resized per cell along with the cell (using springs/struts, i.e. no Auto Layout) and HeightForRow...
The font was setup in Storyboard originally (as system)
This is the same on devices and Simulator
I have a strange behavior in iOS 7. Font is smaller than I expect, if I was set it in to the xib.
If I set font after setting the text it's works for me. Otherwise font is smaller.
Try this:
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
I had the same issue today with UITextView, and while reading AlKozin's answer, I remembered something: somewhere, sometime I read that since iOS 7, the best practice to set font styles is to set them after the View has loaded. In other words, if I set everything in viewDidLoad: , nothing happens. You have to set up the font of your UITextView in viewWillAppear:, like this:
-(void)viewWillAppear:(BOOL)animated
{
self.myTextView.font = [UIFont fontWithName:#"Baskerville-Italic" size:18.0];
}
I ran into a weird bug (Xcode 7.2.1), where unchecking "Selectable" in IB was causing the UITextView to not adhere to the font settings specified through IB.
This is same issue I was facing.
Making UITextview "selectable" from storyboard will work.
Setting textview.font worked fine for me on iOS 7, but none of these answers, or other similar answers on other SO pages worked for me on iOS 8. The only way I was able to get it working was to use an NSAttributedString.
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSTextAlignmentCenter];
tv.attributedText = [[NSAttributedString alloc]
initWithString:text
attributes:#{ NSForegroundColorAttributeName: color,
NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
NSParagraphStyleAttributeName: pStyle,
} ];

Change MFMailComposeViewController's UINavigationBar color in iOS7

I am trying to update my app for iOS7, however I am unable to set the colour of the UINavigationBar at the top of the screen. I have tried the classic approach...
[mailer.navigationBar setTintColor:[UIColor darkGreyColour]];
...however it didn't work. I have also tried calling setBarTintColor instead, however that didn't seem to work either.
Does anyone know if its possible to set the colour and if so how?
I had your same problem when updating my application for iOS 7.
I solved using this line of code (if you use images):
[[UINavigationBar appearance] setBackgroundImage:#"myImage" forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
or this one if you are not using images:
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
I placed then in AppDelegate, just before returning application:didFinishLaunchingWithOptions:
Hope this helps!
Try changing the tint in storyboard. Its in 'view' sub section when you select navigation bar in IB.

iPhone: Possible to change the color of just the prompt of the UINavigationBar?

I'm wondering if I can change the background color of just the prompt of the UINavigationBar (e.g. to red, while the rest of the navigation bar stays black), so I can use the prompt property of the UINavigationItem to display status / error messages as a one-liner.
This is an old question, but since the introduction of appearance there is a much easier way to setting prompt color (and other properties) than adding a subview.
in didFinishLaunchingWithOptions put
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor theColorYouWantYourPromptToBe], UITextAttributeTextColor, otherVariables, nil]];
Nope. But you can set the prompt to an empty string, and place a red-colored label on top of it.