Setting custom font for UILabel appearance causes UIDatePicker bug - ios

In my app delegate I set the font and fontColor of all UILabels using [UILabel apperance], but this is causing the font in my UIDatePicker to also change, which Apple don't allow, and for obvious reasons because it makes the datePicker buggy by changing to default font while scrolling and other inconsistent and unintended behaviour.
How do I ignore UIDatePicker to keep it default when setting all UILabels?

I haven't found a "proper" solution for this problem short of abandoning UIDatePicker or [UILabel appearance], but I did figure out a hack that at least hides the problem. It seems that when the view is initially loaded, the UILabels holding the text for the selected date may be drawn using the label's appearance proxy, but when the UIDatePicker redraws after having the date reset, it will set the label back to the system font.
To hide the problem, in viewWillAppear:, I set the date to two different date values with different date components, then set the picker back to the proper date, like so:
[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:0] animated:NO]; //1970-01-01
[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:49000000] animated:NO]; //1971-07-22
[datePicker setDate:myDate animated:NO]; //Set back to the correct date
It's not elegant, but it makes the date picker look normal again.

The only simple work-around I found was to create the UIDatePicker in code (not using storyboards). I got what I wanted.

Try this ..
[[UILabel appearance] setTextColor:[UIColor redColor]];
[[UILabel appearance] setFont:[UIFont boldSystemFontOfSize:20.0f]];
[[UILabel appearanceWhenContainedIn:[UIDatePicker class],nil] setTextColor:nil];// default is nil (text draws black)
[[UILabel appearanceWhenContainedIn:[UIDatePicker class],nil] setFont:nil];// default is nil (system font 17 plain)
This will change all labels to red colour and their font except the UIDatePicker one. Hope this helps.
Red colour and bold font are for demonstration purposes. They should be as per your requirement.

Related

How to set the text color of the UISearchBar's placeholder

I have a UISearchBar with a dark background color so I was trying to change the place holder text color of UISearchBar (which will be gray by default) but I didn't find a way to set it. So I thought of getting some help :) please suggest me how this can be achieved thanks in advance :)
Note: At the time I wrote this answer I was working on iOS 7 & this workaround worked on iOS 8 as well. It may not work on iOS 9.
Ok it's been two days since I posted this question. Though I din't get the answer I got a workaround for this problem.
Note : I am using storyboard for my application & I have subclassed the UISearchBar and this workaround working like a gem for me.
First and foremost add an appearance proxy to UILabel when its contained in the UISearch bar class like this :
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];//change the color to whichever color needed
Then the most important thing is If you're using storyboard you must and should write some text in the placeholder field of the attribute inspector like this
If you forget to write something in the placeholder field, definitely this trick will not work when you run the application and the placeholder text will look in usual gray color.
Once you're done with writing some text in placeholder field, set the place holder text in your UISearchBar sub class like this :
self.placeholder = #"My Placeholder text";
Now run the application !!! You'll get the place holder text in the color which you have set in appdelegate:) Hope this helps someone :)
Here is the solution
[searchBar setValue:[UIColor blueColor] forKeyPath:#"_searchField.textColor"];

Add UILabel to the left side of a UITextField

I'm trying to consolidate screen space in my iPhone app by putting label text inside of a UITextField that will be uneditable. It will tell the user what to put into the text field. Here is an example of an app that does exactly what I want to do.
I have done my research and found some code to mimic this to some extent.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
label.text= #"Exerise Name:";
label.font = [UIFont fontWithName:#"Arial" size:12];
[label setTextColor:[UIColor blueColor]];
_txtExerciseName.leftViewMode = UITextFieldViewModeAlways;
_txtExerciseName.leftView = label;
[self.view addSubview:_txtExerciseName];
This is inside viewDidLoad. The code yields something like this:
Obviously with styling and tweaking this might work but I was wondering if there is a better way to do this that I am not seeing. If someone has any tips, ideas, or code samples to get me going in the right direction I would really appreciate it.
I saw a really cool solution to this problem. The placeholder text is moved to the top of the textfield when the keyboard comes up/becomes the first responder, then disappears when the textfield resigns the first responder.
Implementations
Objective-C
Swift
The effect you're trying to mimic is almost certainly done by:
Creating what looks like a text box image in an image editing app, importing into your project -- you could also do it via slices so you can create any width field background.
Place the aforementioned image where you want the label and text field
Place a borderless UILabel and a borderless UITextField in the appropriate spot above the image you just dropped.
That should do it. No code changes necessary.

Prevent UiNavigationBar Title from getting Cut off?

I am trying to customize the appearance of the navigationBar title in my ios application. This is the code I currently have:
NSMutableDictionary *navigationTitleAttributes = [NSMutableDictionary dictionary];
[navigationTitleAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[navigationTitleAttributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[navigationTitleAttributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[navigationTitleAttributes setValue:[UIFont fontWithName:#"Calibri" size:30] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:navigationTitleAttributes];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-8 forBarMetrics:UIBarMetricsDefault];
The code yields the following effect:
It works great but my title gets cut off from the bottom.
I've seen solutions to this problem that use a custom UIView (such as this one: UINavigationbar title is cut off when using titleTextAttributes). However, that particular solution requires that the titleView property of the navigation bar be updated for each screen.
I was wondering if there was a simple solution that would cascade through my entire application.
Thanks
Th simple solution is to not use such a large font size. If you set the size to zero then the text should be auto-sized as appropriate.
Otherwise, using a custom view is the correct solution. You can subclass the navigation controller or navigation bar in order to ensure that all of the views have the label styled in the same way.
If you're using a custom font, you may be having the same problem I was. I found a few answers on this post to be quite helpful. I changed my descender values in my .otf font file to prevent my font from being cut off on the bottom. It was especially prevalent in iOS 7.
Custom installed font not displayed correctly in UILabel

appearance proxy not working as intended for UIButton font

im currently styling my app via the appearance proxy and i ran into this problem:
when i set properties on the UIButton appearance my font is ignored:
[buttonAppearance setTitleColor:darkColor forState:UIControlStateNormal];
[buttonAppearance.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:10.0]];
the first line is applied properly (darkColor is some UIColor), but my font change is ignored completely.
When i copy the line into my ViewController and apply it to a concrete button it works fine.
Am i missing something?
any help appreciated! ty
The font name is wrong, it should be HelveticaNeue, without the space between.
In the future if you want to see other iOS font names you should check this website piece of code
EDIT
After a closer look I realized that you are trying to set the appearance of the button's title which is a UILabel, sadly UILabel doesn't have the font property in the UIAppearance proxy and that's why the font doesn't work.
I have found this class TWTButton.h, who resolved my problems adding a new appearance selector [setTitleFont:] to the UIButton class.
buttonAppearance = [TWTButton appearance];
[buttonAppearance setTitleFont:[UIFont systemFontOfSize:10.0f]];
You may read more about this here : http://toastmo.com/blog/2013/01/17/uiappearance/

Some UI elements don't acquire UIAppearance traits

I am trying to use UIAppearance to get a uniform color theme in my iOS app. For example I try to set the text color of all UILabel objects as follows:
[[UILabel appearance] setTextColor:[UIColor colorWithRed:0.7 green:0.07 blue:0.12 alpha:1]];
This works fine for all objects statically defined in my storyboard/XIBs. However, sometimes I need to dynamically create a UILabel in a view. In these cases, the UIAppearance is not used. Instead the default text color (black) is used instead.
Has anyone run into this issue/ found a way around it other than resorting to the old "set every element manually" approach?
Seems that not all the classes support UIAppearance and UILabel is not one of those.
Check this question for more info:
UIAppearance not taking effect on UILabels created programatically
Here's a list of classes that support UIAppearance:
http://blog.mosheberman.com/list-of-classes-that-support-uiappearance-in-ios-5/

Resources