I want to make UILabel's text bold
infoLabel=[[UILabel alloc]initWithFrame:CGRectMake(90,150, 200, 30)];
[infoLabel setText:#"Drag 14 more Flavors"];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont fontWithName:#"Arial" size:16]];
[infoLabel setTextColor:[UIColor colorWithRed:193.0/255
green:27.0/255
blue:23.0/255
alpha:1 ]];
If you want to retain the system font and make it bold:
[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];
Try
[infoLabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:16]];
It may also be worth checking if the font you're trying to use is available on device
Using the GUI in Xcode select the label then go to the Attributes Inspector. One of the options is Font. Click on the font icon (not the up-down arrows). In the popup that appears expand the Font ComboxBox. Under the Bold System section choose Regular.
For swift users this should work:
myLabel.font = UIFont.boldSystemFont(ofSize: 12.0)
or if you'd like to use a different font:
myLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 12.0)
Where possible I would suggest using dynamic font sizes to provide the best possible accessibility to your users.
You can make a label use a system dynamic font and set it to have bold text by doing the following:
exampleLabel.font = UIFont.preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(legibilityWeight: .bold))
You can set the stroke with a negative value to make a bold effect if you don't have the bold variation of your custom font. See here:
How can I both stroke and fill with NSAttributedString w/ UILabel
Related
I want to use different Font name in a UILable in UITableViewCell. I try with a lot of examples like:
Cell.cellSectionLabel.text = [self.textsInTableSection objectAtIndex:0];
Cell.cellSectionImageView.image = [UIImage imageNamed:[self.imagesInTableSection objectAtIndex:0]];
Cell.cellSectionLabel.textColor = [UIColor colorWithRed:59.0f/255.0f green:56.0f/255.0f blue:62.0f/255.0f alpha:1.0f];
[Cell.cellSectionLabel setFont:[UIFont fontWithName:#"Helvetica" size:16]];
Cell.cellSectionLabel.font = [UIFont systemFontOfSize:16];
in cellForRowAtIndexPath
It is not working. The Font name actually read the default Font of my device. Because if I use some different Font name here [Cell.cellSectionLabel setFont:[UIFont fontWithName:#"Baskerville" size:16]];, it doesn't change any thing. It remain the same. How can I use different font name in this case?
Thanks a lot in advance.
You are setting it back to the system font. Delete this line:
Cell.cellSectionLabel.font = [UIFont systemFontOfSize:16];
Please remove the last line of your code and try again.
Cell.cellSectionLabel.font = [UIFont systemFontOfSize:16];
According to your code you are setting font to Helvetica 16
& again changing it back to System font
I am trying to specify the font family of every label in my iOS app in a way that makes it fairly easy to change them later. I don't want to have to go through Interface Builder and reset every font on every screen. According to this post, I have created a method that will find all the fonts in a view and set them accordingly.
In my case, there are a few different font families I need to use based on whether the font is bold, italic, or light (e.g. skinny). These are all located in separate files such as "OpenSans-Semibold.ttf", "OpenSans-Italic.ttf", and "OpenSans-Light.ttf".
Ideally, I would like to be able to set the font to bold, italic, or light in Interface Builder, then have the code override just the font family, using the appropriate .ttf file. According to this post, I can pretty easily detect whether the font has been set to bold or italic, but finding out whether it's light or not doesn't seem to be working.
For the light fonts, the value of "traits" is 0x0--so no flags are set. Is there another way to detect light fonts?
Code looks like this:
- (void) setFontFamily:(UIView*)view
{
if([view isKindOfClass:[UILabel class]])
{
UILabel* label = (UILabel*)view;
UIFontDescriptorSymbolicTraits traits = label.font.fontDescriptor.symbolicTraits;
BOOL bold = traits & UIFontDescriptorTraitBold;
BOOL italic = traits & UIFontDescriptorTraitItalic;
if(bold)
[label setFont:[UIFont fontWithName:#"OpenSans-Semibold"size:label.font.pointSize]];
else if(italic)
[label setFont:[UIFont fontWithName:#"OpenSans-Italic"size:label.font.pointSize]];
else if(light)
[label setFont:[UIFont fontWithName:#"OpenSans-Light"size:label.font.pointSize]];
else
[label setFont:[UIFont fontWithName:#"OpenSans"size:label.font.pointSize]];
}
for(UIView* subView in view.subviews)
[self setFontFamily:subView];
}
Your entire approach to determining a font based on its characteristics is problematic:
else if(italic)
[label setFont:
[UIFont fontWithName:#"OpenSans-Italic"size:label.font.pointSize]];
You are hard-coding the font name based on the trait. Instead, ask the runtime for the font based on the name and trait. In this very simple example I find out what installed font, if any, is an italic variant of Gill Sans:
UIFont* f = [UIFont fontWithName:#"GillSans" size:15];
CTFontRef font2 =
CTFontCreateCopyWithSymbolicTraits (
(__bridge CTFontRef)f, 0, nil,
kCTFontItalicTrait, kCTFontItalicTrait);
UIFont* f2 = CFBridgingRelease(font2);
Note that this code is valid in iOS 7 only, where CTFontRef and UIFont are toll-free bridged to one another. In theory it should be possible to do this without C functions through UIFontDescriptor, but the last time I looked it was buggy and didn't work for all fonts (e.g. Gill Sans!).
That is what you should be doing: determine the symbolic and weight traits of your starting font, and then ask the runtime for the font that most close matches your requirements.
I am creating UILabel, for the label i can set the font name as HelveticaNeue Regular, Light, UltraLight etc, But i unable to set the font name as HelveticaNeue Thin, it is not working as expected. I did like,
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:16];
Also i have searched on Google didnt got any solution. How to fix this issue? Thanks.
This font is bundled with iOS 7, so if you're targeting iOS 7 and above your
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:16.0f];
will work.
However if you are targeting iOS 6.1 and below you'll need to embed the font
Updated answer to support swift
let font = UIFont(name: "HelveticaNeue-Thin", size: 16.0)!
UIFontDescriptor *helveticaNeueFamily =
[UIFontDescriptor fontDescriptorWithFontAttributes:
#{ UIFontDescriptorFamilyAttribute: #"Helvetica Neue" }];
NSArray *matches =
[helveticaNeueFamily matchingFontDescriptorsWithMandatoryKeys: nil];
The matchingFontDescriptorsWithMandatoryKeys: method as shown returns an array of font descriptors for all the Helvetica Neue fonts on the system, such as HelveticaNeue, HelveticaNeue-Medium, HelveticaNeue-Light, HelveticaNeue-Thin, and so on.
You can modify the fonts returned by preferredFontForTextStyle: by applying symbolic traits, such as bold, italic, expanded, and condensed. You can use font descriptors to modify particular traits, as shown in Listing 9-2.
referenced by apple
or
this font you can't apply directly.
so you can customize your font
How to use custom fonts in iPhone SDK?
This has worked for me. Write this code below your label declarations.
It sets all the UILabel under a function with same font.
[[UILabel appearance]setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:32.0f]];
To set font to particular UILabel use this code :
[labelName setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15.0f]];
I have a UILabel. Its name, size and other properties are set in IB.
I wish to change the font size if the text is longer than lets say 100.
All the examples I saw here I have to supply the text name but I'm looking for something of the sort:
label.font.size = 15;
I can't user [UIFont fontWithName:#"Arial" size:13.0f]] nor [UIFont systemFontOfSize:25]
I'm sure there must be a way to change only the size
label.font = [label.font fontWithSize:15];
label.font = [UIFont fontWithName:label.font.familyName size:15];
You should look at minimumFontSize and minimumScaleFactor depending on which versions of iOS you need to support.
I have copy my ttf font (ABeeZee-Italic.ttf) from Google WebFonts into Supporting Files and added into plist :
and assign my Label with tag = 4 :
because I have this code to embed fonts into UILabel:
for (UILabel *customLabel in [[self view] subviews]) {
if (customLabel.tag == 1) {
[customLabel setFont:[UIFont fontWithName:#"Raleway-ExtraLight" size:21]];
} else if (customLabel.tag==2){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
} else if (customLabel.tag==3){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:11]];
} else if (customLabel.tag==4){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Italic" size:12]];
}
}
it's all working like a charm, but the italic font is not loaded into my iPod (iOS 6.1.3). UILabel with tag 4 is the iOS system font. not ABeeZee-Italic.
I tried to change the tag number, but also failed. Is there any specific requirements for non-regular font to be embedded into iOS?
thank you
UPDATE #iPatel answer : here's how my FontBook looks like, it has same name...
Check what is real name of font (ABeeZee-Italic) in "FontBook" ? and write name of font which is in "FontBook".
And use such like
[myLabelName setFont: [UIFont fontWithName:#"Font Name From Font Book" size:32]];
More information about how to add custom font in app. then read This Answer.
EDITED:
Also try with
Select Your font from your project
right click
Open With External Editor
Install Font
select inserted font
And check what is "Full name", copy and past it in your code:
May be helpful :)
You can do this in two ways.
Using ttf file
[customLabel setFont:[UIFont fontWithName:#"Raleway-ExtraLight.ttf" size:21]];
or
Using font name
[customLabel setFont:[UIFont fontWithName:<Name of Raleway-ExtraLight> size:21]];
In your case just add .ttf in setFont: else get Name of font as shown in below image.
Just right click on .ttf file > Get Info > Full Name (your font name. You can use this name as font name in setFont)
EDIT
Check the full name here and paste it in your code.
[customLabel setFont:[UIFont fontWithName:#"ABeeZee Italic" size:21]];
EDIT 1
May be you need to Register Fonts. Just try to register and check.