Using a custom font with bold text in a UILabel - iphone

I have an app where I am setting the font for a UILabel programmatically as below
[label setFont:[UIFont fontWithName:fontName size:[fontSize intValue]]];
wherein fontName is a string type variable that hold font name such as "Helvetica"
and fontSize will hold the size of the font. This works fine for me.
Now I want to make this text "Bold" how can I do that?
boldSystemFontOfSize
works for system font. How can I achieve the same for user defined fonts ?
Thanks.

Try using #"Helvetica-Bold" as the fontName.

Try this,
NSArray *arr = [UIFont fontNamesForFamilyName:#"myFavoriteFont"];
NSString *fontName = [arr objectAtIndex:0]; //or [arr objectAtIndex:1], it depends
[label setFont:[UIFont fontWithName:fontName size:[fontSize intValue]]];

Try,
fontName = #"myFavouriteFont-Bold";
[label setFont:[UIFont fontWithName:fontName size:[fontSize intValue]]];
By using the the method fontWithName:size:, you can set the font name with style. Doc says that the fontName is the fully specified name of the font and this name incorporates both the font family name and the specific style information for the font.

Related

Font size issue in iOS

I am trying to set the font of the label as per follow but it gives me zero CGSize for that.
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica Cyrillic Bold_5" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle sizeWithFont:abbrFont];//_addbrTitle is a NSString
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)
Help me to solve this.
Thank you.
Try this,
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //E
You can use this line to set the size for label..
[label setFont:[UIFont fontWithName:#"Helvetica" size:20.0]];
UILabel *_addbrTitle = [[UILabel alloc] init];
[_addbrTitle setText:#"Hello"];
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
[_addbrTitle setFont:abbrFont];
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)
Font name you have entered in the code does not exists in system. So its printing (0,0) every time. Enter a font name which exists in system it will print the size.
change your font name to some other built in font name then try
like this
yourLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
Your font name is not current follow this IOS supporting fonts list and Font name
http://iosfonts.com/
NSURL *fontURL = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"your_font_name" ofType:#"TTF"]];
for example:
NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"segoe_semi_bold" ofType:#"TTF"]];
NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"segoe_semi_bold" ofType:#"TTF"]];
now make add url to array
NSArray *arrFont =[[NSArray alloc]initWithObjects:fontURL1,fontURL2, nil];
int result =CTFontManagerRegisterFontsForURLs((CFArrayRef)arrFont, kCTFontManagerScopeUser, nil);
if(result)
{
NSLog("Font Install successfully");
}
now you can get font

Not able to change the text size in a label when clicking a button

In my iphone app. I want to change the text size in a label when clicking a button
Create UILabel
self.lbl=[[UILabel alloc] initWithFrame:CGRectMake(135, 290,200,35)];
self.lbl.backgroundColor=[UIColor clearColor];
NSString *str=[NSString stringWithFormat:#"%.f",[self.slider value]];
[self.lbl setFont:[UIFont fontWithName:#"Arial-BoldMT" size:16]];
self.lbl.text=str;
[self.View addSubview:self.lbl];
Change size of text of UILabel when tapped UIButton
-(IBAction)ButtonPressed:(id)sender
{
[self.lbl setFont:[UIFont fontWithName:#"Arial-BoldMT" size:25]];
}
Here is how to change the font size at runtime....
yourLabel.font=[UIFont systemFontOfSize:14];
-(IBAction)changeFontOfLabel:(id)sender
{
[self.lebelObject setFont:[UIFont fontWithName:#"American Typewriter" size:18];
}
and also write the proper font name . check here for a list of fonts

Unable to use Custom Font in UIButton

I am trying to use a custom font in my application, which is of ttf format. In the Project settings, I have addd that font name like below .
And I have added that font in my resources folder like below
And the font Which I am trying to use is this one
_enterButton.titleLabel.font = [UIFont fontWithName:#"Venus Rising" size:45.0];
If I use Arial or some other font, it works fine. But when I am trying to use this font, it is not at all working. Can anyone tell me why ??
I downloaded the font and as I thought it has different name than what is the name of the file. Look at this image.
Also don't forget on adding the name to the Info.plist of your app, under the key "Fonts provided by application". Hope it helps! :)
You missed extension of font in info.plist, I mean in Info.plist, "Fonts provided by application" you need to set "Venus Rising.ttf" then your problem will be solved
I encountered the same problem few days ago. For me the the font name was different from the ttf file name.
Try this loop to check all the font available in your app :
for (id familyName in [UIFont familyNames]) {
NSLog(#"family name : %#",familyName);
for (id font in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#" %#",font);
}
}
In your case, Venus Rising should appear as a family name but the exact name of the font you should use in your code may be VenusRisingRegular or something like that.
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 240, 40)];
[label1 setFont: [UIFont fontWithName: #"Grinched" size:24]];
[label1 setText:#"Grinched Font"];
[[self view] addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 240, 40)];
[label2 setFont: [UIFont fontWithName: #"Energon" size:18]];
[label2 setText:#"Energon Font"];
[[self view] addSubview:label2];
Please try this.
It's may help to you

Font bold issue in iphone

I have a label and i want make its font bold pragmatically.
I am trying this code.
[label1 setFont: [UIFont fontWithName:#"Helvetica LT Compressed" size:14.0 ]];
[label1 boldSystemFontOfSize:14.0];
Bold fonts have another name that regular or italic ones.
If you want to know the exact name of a font, I suggest you to download the "Fonts" iphone application, on the appStore. It's free and it references every possible font that your phone handle.
It's not my app, it just an app that every iphone developer should have.
...and for helvetica, you have :
#"Helvetica"
#"Helvetica-Bold"
#"Helvetica-BoldOblique"
#"Helvetica-Oblique"
label1.font = [UIFont boldSystemFontOfSize:14];
label1.font = [UIFont fontWithName:#"Helvetica LT Compressed" size:14];
You will have to Use bold version of the font,
[label setFont:[UIFont fontWithName:#"Helvetica-Bold" size :34];

UIButton font Size Not Changing

I am changing UIButton font size but it does not effect.
UIButton*headingButton=[UIButton buttonWithType:UIButtonTypeCustom];
headingButton.frame=CGRectMake(182, 4, 450, 42);
[headingButton setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];
I cant see in this code that u are changing font size but anyway, this can be done like so:
[headingButton.titleLabel setFont:[UIFont fontWithName:<your font name> size:<your desired size>]];
or
[headingButton.titleLabel setFont:[UIFont fontWithName:systemFontOfSize:<your desired size>]];
Update: plus you also need to see if the font u are setting exists in your app to check just do this:
NSLog(#"Font family names: %#", [UIFont familyNames]);
NSLog(#"Fonts in family Myriad Pro : %#", [UIFont fontNamesForFamilyName:#"Myriad Pro"]);
If it doesnt exist, you need to drag the otf file into ur project and also add the "Fonts provided by application" in info.plist
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:8.0];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:btn.titleLabel.font.pointSize+3];
first Line for set font name and size and second line for bold a font