Crash in ios7 while setting font on navigation bar - ios

I am using this link here to change the font and color of title in navigation bar. I also tried this. Both of them run fine in ios8 but when i test in ios7 each time it crashes at "NSFontAttributeName". If i remove the code to set the font and only set the color , it works.
The crash message that I get is * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]
Any help would be appreciated. Hope my problem is clear.

Use this code:
UINavigationBar *navBar = [UINavigationBar appearance];
[navBar setTitleTextAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"yourFont" size:15]];

Try this, it works fine for me in both IOS8 as well as IOS7
`
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor clearColor];
shadow.shadowOffset = CGSizeMake(0, 0);
[self.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName:#"custom-color",
NSShadowAttributeName: shadow,
NSFontAttributeName: #"font-name",
}];`

Related

How Properly To Set Font for All Labels in View Controller Using UIAppearance

I am trying to set the font of labels in the whole app (or at least labels controlled by View Controller) the same, therefore, I chose the option of creating category like it is recommended here or here. For these purposes I created the method "setSubstituteFont" in the UILabel category. However, the syntax
[[UILabel appearance] setSubstituteFont];
gives me always the same error if I put that statement in my View Controller:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSMethodSignature
getArgumentTypeAtIndex:]: index (2) out of bounds [0, 1]'
I prefer to implement that feature in my View Controllers since my Views are .xib, therefore, the recomendations to use "setNeedsDisplay" as here are not an option.
I also tried other options like:
[[UILabel appearanceWhenContainedIn:[UIViewController class], nil] setSubstituteFont];
or
[[UILabel appearanceWhenContainedIn:[self class], nil] setSubstituteFont];
or
[[UILabel appearanceForTraitCollection:self.traitCollection] setSubstituteFont];
since "appearanceWhenContainedIn" is deprecated in iOS9 according the documentation but still getting the same error, and the app is crashed.
Between, "setSubstituteFont" method looks like that:
- (void)setSubstituteFont UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:#"GothamRounded-Book" size:54.0];
}
The used selector was taken from the list.
Try this
#interface UILabel (UIAPP)
- (void)setSubstituteFont:(UIFont *)font UI_APPEARANCE_SELECTOR;
#end
#implementation UILabel (UIAPP)
- (void)setSubstituteFont:(UIFont *)font {
if (font == nil) {
self.font = [UIFont fontWithName:#"GothamRounded-Book" size:54.0];
} else {
self.font = font;
}
}
#end
Call where you need even in View Controller:
[[UILabel appearance] setSubstituteFont: nil];

Unrecognized selector sent to instance 0xa44bef0

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[UISlider
setPopUpViewCornerRadius:]: unrecognized selector sent to instance
0xa44bef0'
Code:
self.slider1.maximumValue = 255.0;
self.slider1.popUpViewCornerRadius = 25.0;
[self.slider1 setMaxFractionDigitsDisplayed:0];
self.slider1.popUpViewColor = [UIColor colorWithHue:0.55 saturation:0.8 brightness:0.9 alpha:0.7];
self.slider1.font = [UIFont fontWithName:#"GillSans-Bold" size:22];
self.slider1.textColor = [UIColor colorWithHue:0.55 saturation:1.0 brightness:0.5 alpha:1];
Please guide me, Thanks in Advance
UISlider doesn't have this property popUpViewCornerRadius, because of that this error is occurring. You will need to remove this below line.
self.slider1.popUpViewCornerRadius = 25.0;
When you are allocating self.slider you need to use ASValueTrackingSlider instead of UISlider.
if you are using story board or xib you need to give ASValueTrackingSlider in identity inspector. like
http://cocoadocs.org/docsets/ASValueTrackingSlider/0.9.2/Classes/ASValueTrackingSlider.html refer the link.
UiSlider does not have this property of
popUpViewCornerRadius
Either remove this line else this property would be included in the Custom class.
So change the UISlider *slider1 to ASValueTrackingSlider *slider1

programmatically set UILabel font to User Defined Runtime Attribute

I have an UIViewController in which I create an UILabel programmatically in its' viewDidLoad: like so:
- (void)viewDidLoad
{
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 0, 100, 100)];
navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor colorWithHexString:#"#000029"];
[navTitleLabel setFont:[UIFont fontWithName:#"Pennyscript" size:20]];
self.navigationItem.titleView = navTitleLabel;
}
I have the "User defined Runtime Attribute" set in the ViewController's identity inspector tab:
What I'm trying to do here is programmatically set the UILabel navTitleLabel's font to my custom font, then add that UILabel into the navigationBar's titleView. What am I doing wrong? How can I accomplish this goal?
With the UDKA(User defined key attribute) in the VC ID-inspector, the app crashes with this stack trace message:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fontName.'
When I remove my User Defined Key Attribute from the ViewController, I can then navigate to the VC with no crash.
In your View Controller define a property as:
#property (nonatomic, copy) NSString *myFontName;
And in the view controllers Identity inspector change the Keypath from fontName to myFontName.
And change this code:
[navTitleLabel setFont:[UIFont fontWithName:#"Pennyscript" size:20]];
to:
[navTitleLabel setFont:[UIFont fontWithName:myFontName size:20]];
Tutorials:
Reference
Reference
Reference
Reference
The exception that you get is because you try to set a property (this is what setValue:forKeypath: is doing) of UILabel. Since you pass the string fontName as keypath, iOS attempts to set the property fontName of UILabel to the value NavBar Title. This is why you are getting the exception, because UILabel doesn't have a property called fontName! Calling [label setFont:[UIFont fontWithName:#"Pennyscript" size:20]]; should be enough!
Copy font in project File. add in project then add in info.plist
then put this methods
UILabel *navTitleLabel = [[UILabel alloc]
initWithFrame:CGRectMake(17, 0, 100, 100)];
navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.text = #"hello";
navTitleLabel.font = [UIFont fontWithName:#"Pennyscript" size:navTitleLabel.font.pointSize];
self.navigationItem.titleView = navTitleLabel;

Change color of navigation bar ios6 and ios7

I'm in the process of upgrading my app to iOS7. However I want to preserve the iOS6 interface as well. It works well except for the navigation bar(s). In iOS7 they look great (just the default color with the translucent property to YES. In iOS6 the navigation bars are showing as the default blue bars and I want them to be black translucent.
What I do is check the version of the iOS and then perform some code. In the debugger I see they right version in the vComp variable but the color is not changing. Don't mind the redColor property, that's just for the test. In both ways I am presented with the default color.
Here is my code:
- (void) fixNavBarColor:(UINavigationBar*)bar {
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[vComp objectAtIndex:0] intValue] >= 7) {
bar.barTintColor = [UIColor redColor];
bar.translucent = NO;
}
else {
bar.tintColor = [UIColor redColor];
bar.opaque = YES;
}
}
There is no error or warning.
Any ideas?
You shouldn't set the tintColor straight to navigationBar as it wouldn't be applied to other parts of your app, you should instead use UINavigationBar's appearance to set tintColor which is available on iOS 5.0 onwards.
[[UINavigationBar appearance] setTintColor:"Your Color"];
Use this code for iOS6
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0 alpha:.8]]
forBarMetrics:UIBarMetricsDefault];

UITextField __CFStringEncodeByteStream EXC_BAD_ACCESS

I have a strange exception on a UITextField. I solved it but have no clue as to why it does.
After running the application I set the UITextField appearance as follows:
UITextField *textfieldAppearance = [UITextField appearance];
textfieldAppearance.font = [_theme textfieldFont];
textfieldAppearance.textColor = [_theme textfieldColor];
if (textDirectionRTL) {
textfieldAppearance.textAlignment = NSTextAlignmentRight;
}
textfieldAppearance.backgroundColor = [UIColor clearColor];
As you can see, there's nothing unusual about it, but when the controller with the UITextField opens I get a __CFStringEncodeByteStream + 17 EXC_BAD_ACCESS exception.
The solution was to comment out the backgroundColor setting.
I have the exact same appearance setting on UITextView and everything's ok.
Moreover, when I set the UITextField backgroundColor in the code within awakeAfterUsingCoder, everything works like a charm.
I'm running the app in XCode Version 4.6.3 (4H1503) simulator
Any clue to this strange behavior?
This is crashing because you are using the appearance proxy, which doesn't support all customisations. It doesn't seem to support backgroundColor so you will have to modify that on each text field individually
First of all why are u using UIAppearance class to customize the UItextfield..?? All the properties u are trying to set are present in UItextField itself...!
textField.backgroundColor = [UIColor clearColor];
textField.textColor = [UIColor redColor];
textField.textAlignment = UITextAlignmentCenter;
textField.font = [UIFont fontWithName:#"font name" size:12.0];
//Set the properties as you want.
Refer here for more

Resources