objective-c changing UILabel's font not work - objective-c

i want to change my label's font using below code:
UILabel *lblBolge = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 38)];
lblBolge.text=#"Bölge";
lblBolge.font=[UIFont fontWithName:#"CarterOne" size:10];
but still i see system font and default size
any reason?

Carter font is not part of the iOS SDK. To add a custom font you need to get hold of the .ttf or .otf file of the font and add it to your project. Here's a tutorial on how to do that.

Related

UINavigationBar custom font not working on iOS 8

I've spent some hours trying to use a custom font for the navigation bar title on iOS 8. This works ok on iOS 7:
UIFont *font = [UIFont fontWithName:#"Gotham-Bold" size:12];
NSDictionary *textAttributes = #{ NSFontAttributeName: font };
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
But on iOS 8 the text simply disappears. Changing the color, or the system font size works perfectly, but trying to use a custom font won't work at all. Anyone has any idea on this?
Thanks!
EDIT
I've created a small project so you can try it yourself:
https://github.com/noquepoaqui/customHeaderFont
These lines of code are on MasterViewController on line 30.
You have a typo when you assign the attributes:
It should be:
self.navigationController.navigationBar.titleTextAttributes
instead of:
self.navigationController.navigationBar.TitleTextAttributes
If that doesn't fix the issue, you can test if the font was added correctly by placing this code in the AppDelegate's didFinishLoadingWithOptions. This will print all available fonts of your app. Just look at the output window and search for your font name (Gotham-Bold). If it's not listed you can delete the font from your project and add it again via drag & drop. Make sure to tick "add to target" next to your app in the dialog that appears.
//list all available fonts
for (NSString *family in [UIFont familyNames]) {
NSLog(#"---------- %# ----------", family.uppercaseString);
NSArray *names = [UIFont fontNamesForFamilyName:family];
for (NSString *font in names) NSLog(#"%#", font);
}
The .ttf font i was using was not working well. I've installed the .otf version and everything works well.

only change font size (and not font name)

What I want to do is change the font size.
I know below statement will change the font size, but it changes the font name because we are using systemFontOfSize
[UIFont systemFontOfSize: 13.0];
I know alternate option is as mentioned below.
[myLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
but I don't want to use fontWithName as I am setting that in IB.
I don't want to play with font name as my app is multi-language and hence I don't want to play with font name.
Any idea how can I just change fontsize and don't change the fontname.
Too bad that the font metrics properties of UIFont are readonly. It'd be nice if they could be adjusted dynamically without having to do this below:
UIFont * fontFromLabel = myLabel.font;
// now we have the font from the label, let's make a new font
// with the same font name but a different size
if(fontFromLabel)
{
// 13, or whatever size you want
UIFont * newFontForLabel = [UIFont fontWithName: fontFromLabel.fontName size: 13.0f];
if(newFontForLabel)
{
[myLabel setFont: newFontForLabel];
}
}

How can I change the font size of a uilabel without changing the font itself?

I'm having an issue where I allow the user to select a font for a label in my project, but then when the user sets the size of that label (using a different button), the label resets to a default font. I'd like to be able to retain the font that the user had applied while still allowing the user to change the font size. Any help is appreciated, thanks!
Here's my code..
-(IBAction)setFont{
[userText setFont:[UIFont fontWithName:#"Arial-BoldMT" size:50.0]];
//I had to add a size when setting the font or else I got an error
}
-(IBAction)setFontSize{
[userText setFont:[UIFont systemFontOfSize:24]];
}
Just use the fontWithSize: method on the label's current font:
- (IBAction)setFontSize {
// Keep the same font but change its size to 24 points.
UIFont *font = userText.font;
userText.font = [font fontWithSize:24];
}
The function [UIFont systemFontOfSize:] will always return default system font. You can just make use of the same function that you call in setFont which is [UIFont fontWithName:size:].

Setting size to custom iOS font doesnt work

I am using some custom fonts in my iphone app. They are displayed correctly, but i cant change their size.
Here is how set this font for UILabel:
myLabel.font=[UIFont fontWithName:#"Cabin-Regular" size:18];
This label is part of uitableview cell, if that could play any role.
Label settings in IB:
I change only color, text and font in code for this label.
What could be wrong?
Are you sure the font is loaded correctly?
You can try:
for (NSString *familyName in [UIFont familyNames]) {
NSLog(#"%#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"\t%#",fontName);
}
}
This will show you all the fonts that are loaded. Make sure the same name "Cabin-Regular" is showing up exactly the same. A lot of times the font itself isn't loaded and then the font-size won't be used at all.
You need to add your font in your Xcode, if you are adding manually.
Add your font in your project files
Go to your Project name-Info.plist file, right click and click Add Row.
In your new row type Fonts provided by application. If you expand it you can add new items, in there add value to item 0 . The value should be your font file name. like below image
4. Double click on your font, it'll open in external editor, like following image
5.Give that name into your fontWithName: method. If you are setting value to your tableviewcell. the code will be
cell.nameLbl.font = [UIFont fontWithName:#"Cabin" size:18.0];
It works fine for me. I hope it helps :)

Color of custom font in navgation controller (setTextColor): gray instead of white

I am trying to set custom font and color (white) for font in my app's navigation bar title. Instead of white I get gray text color.
Here is the code I'm using to set custom font:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
label.textAlignment = UITextAlignmentCenter;
label.adjustsFontSizeToFitWidth = NO;
label.minimumFontSize = 10.0;
[label setFont:[UIFont fontWithName:#"HelveticaNeueLTCom-MdCn" size:24.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:#"View title"];
[self.navigationItem setTitleView:label];
Same thing happened when I tried to set custom font color in UITableView sections.
I know about some problems with Helvetica Neue in iPhone apps development but the color of this font works well in other parts of my projects.
Am I missing something here?
Have you tried other .ttf fonts just to test if they work? I wonder if there is something wrong with the font file itself. try this directory and see if it works.
The problem was that I've (deliberately) overriden default font for the whole application as described in this answer.
The solution was to remove this override (all the #import's and the remove .m file from the target, or just delete both of the class files.