0

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.

Chizx
  • 351
  • 2
  • 5
  • 16
  • Did you add the font to your project in the _Info Plist_ file? And the corresponding _.ttf_-files? – nburk Jul 24 '14 at 05:20
  • Yes. I did that already. I have already used the font on UILabels in the storyboard.. but this UILabel is programmatic so I am confused. – Chizx Jul 24 '14 at 05:20
  • hmm, usually you wouldn't have to define the _User defined attribute_ then either... it should be sufficient to use the font in code just as you do. – nburk Jul 24 '14 at 05:22
  • are you sure _Pennyscript_ is the correct name of the font? you can check the proper font names of the custom font using the answer of this question: http://stackoverflow.com/questions/15984937/adding-custom-fonts-to-ios-app-finding-their-real-names – nburk Jul 24 '14 at 05:25
  • the _user define runtime attribute_ and the line `[navTitleLabel setValue:@"NavBar Title" forKeyPath:@"fontName"]` are superfluous, just setting the font of the label is enough by the way. – nburk Jul 24 '14 at 05:27
  • Yes I know how to print the fonts, it is in there, and is most definitely "Pennyscript", I have already used the font on several `UILabels` and `UIButtons`, my program is crashing when loading the VC that I'm trying to use it in, I think the problem is that the *User defined Key Attribute* is in the VC's id-inspector rather than in the UILabel's id-inspector, and oh.. I didn't know that, I was just trying to get it to work – Chizx Jul 24 '14 at 05:28
  • why are you using _user define attributes at all_? can't just you remove that line? what's its purpose here? – nburk Jul 24 '14 at 05:31
  • It's the only way to actually recognize the .otf file from the .plist file. It doesn't work without the *User Defined Runtime Attribute* being declared. – Chizx Jul 24 '14 at 05:34
  • hmm, that's strange. i am using custom fonts in my apps and i never had to do this to recognize any font. are you doing this in all the views where you use the font? – nburk Jul 24 '14 at 05:39
  • Yes I am using it in all of the views, but only in the identity inspector of the UILabel's and UIButton's that I need the font for. – Chizx Jul 24 '14 at 05:41
  • i still dont get what this line does: `[navTitleLabel setValue:@"NavBar Title" forKeyPath:@"fontName"]`. why are you setting tthe label's `fontName` property to **NavBar Title** – nburk Jul 24 '14 at 05:41
  • I'm not really sure to be honest I was just trying to get the font to work lol. – Chizx Jul 24 '14 at 05:45
  • remove the line `[navTitleLabel setValue:@"NavBar Title" forKeyPath:@"fontName"]`, it doesn't make any sense and it causes your crash! you also don't need user define runtime attributes! – nburk Jul 24 '14 at 05:48

3 Answers3

2

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:

  1. Reference
  2. Reference
  3. Reference
  4. Reference
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
1

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!

nburk
  • 22,409
  • 18
  • 87
  • 132
  • Sorry this isn't right... I removed the "*User Defined Key Attribute*" and that fixed the crash... I will update code., it is still not working though.. I must give the UILabel the UDKA somehow.. but programmatically. – Chizx Jul 24 '14 at 05:49
  • Believe me, **setting custom fonts has nothing to do with user defined runtime attributes**! Check the documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UIFont_Class/Reference/Reference.html – nburk Jul 24 '14 at 05:52
  • does it work when you just set the system font in a smaller size? are the changes taken? you need to take small steps and start debugging your issue further., try e.g.: `[navTitleLabel setFont:[UIFont systemFontOfSize:14]];`. does this work and your font is getting smaller? if not you know that your issue is not because of the font, but because the label is not set properly. – nburk Jul 24 '14 at 05:59
  • Oh wow... sadd, the problem is that I was never setting the UILabel's text.. haha. It's totally working... with no need of the User Defined Key Attribute – Chizx Jul 24 '14 at 06:04
  • what happens when you print out the font of the `navTitleLabel ` with `NSLog(@"Font: %@", navTitleLabel.font);` after you set the font with `[navTitleLabel setFont:[UIFont fontWithName:@"Pennyscript" size:20]];`, does it actually tell you that the font has been set correctly? – nburk Jul 24 '14 at 06:04
0

Copy font in project File. add in project then add in info.plist

enter image description here

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;
Hardik Kardani
  • 576
  • 3
  • 24