programmatically set UILabel font to User Defined Runtime Attribute - ios

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;

Related

Objective c - new UILabel text is 'merged' with the existing

I am refactoring an 'old' Objective-C iOS-program. All layout is done programmatically without storyboard.
I am experiencing some odd behavior when I try to update the text on a UILabel. The text is NOT updated but written 'on top' of the existing text.
The context is:
The UILabel is defined in a 'top-bar' that is managed by a sub-ViewController. It is assigned as 'default'/placeholder value when initialized by the sub-ViewController.
lblArea = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
//self.lblArea.text = #"giraf";
lblArea.textColor = BLACK_COLOR;
lblArea.backgroundColor = [UIColor clearColor];
lblArea.textAlignment = NSTextAlignmentCenter;
lblArea.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14];
//self.lblArea.text=SEARCH_FOR_ADDRESS;
NSAttributedString *placeholderText = [[NSMutableAttributedString alloc] initWithString:SEARCH_FOR_ADDRESS attributes : #{
NSForegroundColorAttributeName : GRAY_COLOR,
}];
lblArea.attributedText=placeholderText;
[self.view addSubview:lblArea];
The setter-method is
-(void)setLabelAreaText:(NSString *)labelText{
lblArea.text=labelText;
}
In the parent controller the sub-controller is added as follows:
[self addChildViewController:topBarViewController];
[topBarViewController didMoveToParentViewController:self];
[self.view addSubview:[topBarViewController view]];
When the label is assigned another value from the parent-ViewController (via a setter method) the text is not replaced but written 'on top' of the existing.
The UILabel is defined as a instance variable and is made accesiable via a custom setter-method. I have tried to change it to a public property but it doesn't change anything.
Can anyone please help with an explanation :-)

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];

iOS, UIBarButtonItem: Custom label bound to UIBarButtonItem

I have an issue with attaching a custom label bound to my UIBarButtonItem. My code:
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0.00, 0.00, 10.0, 10.0)];
lbl.font=[UIFont fontWithName:#"Verdana" size:7];
lbl.textAlignment=NSTextAlignmentCenter;
lbl.textColor=[UIColor darkGrayColor];
lbl.text=#"Subtypes available:";
lbl.backgroundColor=[UIColor lightGrayColor];
_btnSubtypes.enabled=YES; // UIBarButtonItem
_btnSubtypes.title=[NSString stringWithFormat:#"%lu / %lu",[_ucqc returnQuantityOfListedSubtypes:_coin],[_ucqc returnPureQuantityOfSubtypesInType:_coin]];
[_btnSubtypes setAction:#selector(navigateToSubtypes)];
[_btnSubtypes.customView addSubview:lbl];
This code is executed in viewWillAppear method. So, it doesn't set label. What's wrong with it?
Thank you in advance.
Hi I think is better in order to custom use UIButton than UIBarButtonItem. In your code you don't have any customView. If you insert a NSLog statements you can see than is nil.
[_btnSubtypes.customView addSubview:lbl];
NSLog(#"have I go customView? %#",[_btnSubtypes.customView description]);
You should change this:
_btnSubtypes.customView=lbl;
But It´s posible the result not match with you expect.

Cannot make programmatically created UITextField editable (iOS)

I created a UITextField programatically, (I do not use storyboard) and added it as a subview to ViewController with this code:
#interface ViewController ()
#property (nonatomic, strong) UITextField *searchLocationBar;
#end
...
#synthesize searchLocationBar;
...
self.searchLocationBar = [[UITextField alloc] init];
self.searchLocationBar.frame = CGRectMake(0.0f, 0.0f, 320.0f, 40.0f);
self.searchLocationBar.delegate = self;
self.searchLocationBar.borderStyle = UITextBorderStyleRoundedRect;
self.searchLocationBar.placeholder = #"a temporary placeholder";
self.searchLocationBar.userInteractionEnabled = YES;
self.searchLocationBar.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:self.searchLocationBar];
However, I cannot enter any text - nothing happens, when I tap on a textfield. It's not overlapped by any other view.
I've checked UITextfield not editable-iphone but no effect
I'm newbie and totally sure I simply miss something - please advice.
Thanks!
EDIT:
One more thing: I have a Google Maps GMSMapView assigned to self.view as
self.view = mapView_; as written in Google API documentation.
After some tests I found that with this declaration all controls work perfectly, but not textfields. I would prefer not to move a map view to any subview as I will need to rewrite lots of things.
Can someone please add any suggestions?
you forget add:
[self.view bringSubviewToFront:self.searchLocationBar];
In Xcode 5 your code should work.Better you check your Xcode version.May be the problem with your code with Xcode versions.You can try by following way.
UITextField *lastName = [[[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];
[self.view addSubview:lastName];
lastName.placeholder = #"Enter your last name here"; //for place holder
lastName.textAlignment = UITextAlignmentLeft; //for text Alignment
lastName.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:14.0]; // text font
lastName.adjustsFontSizeToFitWidth = YES; //adjust the font size to fit width.
lastName.textColor = [UIColor greenColor]; //text color
lastName.keyboardType = UIKeyboardTypeAlphabet; //keyboard type of ur choice
lastName.returnKeyType = UIReturnKeyDone; //returnKey type for keyboard
lastName.clearButtonMode = UITextFieldViewModeWhileEditing;//for clear button on right side
lastName.delegate = self; //use this when ur using Delegate methods of UITextField
There are lot other attributes available but these are few which we use it frequently.if u wanna know about more attributes and how to use them refer to the following link.
You can also make property for UITextField.Either way should work fine in Xcode.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html

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