Maybe I'm looking at the wrong place, however how do I set an UILabel's font and AND its weight?
Looking at the documentation, there seems to be only methods to create an UIFont with a given font name and size, like
[UIFont fontWithName:#"Helvetica" size:22])
OR create a bold font, with
[UIFont boldSystemFontOfSize:22]
How can I use these both together?
The documentation for fontWithName:size: states, "...name incorporates both the font family and the specific style information for the font."
So you probably want:
[UIFont fontWithName:#"Helvetica-Bold" size:22];
fontNamesForFamilyName: is handy for getting a list of available fonts for a given family.
For example:
NSArray* fontNames = [UIFont fontNamesForFamilyName:#"Helvetica"];
for( NSString* aFontName in fontNames ) {
NSLog( #"Font name: %#", aFontName );
}
...which outputs:
Font name: Helvetica-BoldOblique
Font name: Helvetica
Font name: Helvetica-Oblique
Font name: Helvetica-Bold
Related
For the first time i decided to use NSAttributed string for my UILabel,
nevertheless when i set my UILabel attributes (font, color, alignement) of my labels in IB (interface builder interface) when i run my code the attributes are never respected ! I have to manually add attributes to my labels :
_myLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:25];
Can someone help me ?
What you're doing is applying a font font the text property of the label. If you'd like to assign a font for attributed text, you have to assign that font as one of the attributed string's attributes.
NSString *inputString = #"fsdfdsf";
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"Arial-BoldMT" size:25]};
NSAttributedString *attributecString = [[NSAttributedString alloc] initWithString:inputString attributes:attributes];
[_myLabel setAttributedText:attributecString];
How to get the font style of UILabel? Whether it is Bold or italic ?
You will need to inspect the font name.
myLabel.font.fontName
http://developer.apple.com/library/ios/documentation/uikit/reference/UIFont_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006891-CH4-SW16
Try this one:
NSAttributedString *string=yourLabel.attributedText;
if ([[textFieldFontName rangeOfString:string] length] > 0){
NSLog(#"Bold");
}
if ([[textFieldFontName rangeOfString:string] length] > 0){
NSLog(#"Italic");
}
*Note if your string contains both then, this may not work
Select your label in the interface builder (xib or storyboard) and go to the Attributes Inspector. There click on the "T" by font and then set font to Custom.
Now you can change the font, size etc.
UILabel *myLabel;
myLabel.font = [UIFont fontWithName:#"fontName" size:16.0];//set font according your choice
myLabel.font = [UIFont boldSystemFontOfSize:16.0];//bold font
myLabel.font = [UIFont systemFontOfSize:16.0];//system font
Hope it's helpful for you....
i have to add custom font file .Name of file if #"sample font.ttf".I do following steps.
Drop the file (sample font.ttf) into your project. Open up your Info.plist file, create a key called UIAppFonts and make it an array. Add the filename of the font as a value
But it not working ..I thought the reason may by space in filename.when i get this file it was a compressed with filename #"sample_font.ttf".when i decompress it ,it removes #"_" and get "sample font.ttf"
Then i install it in font book .the name in window is first word file that is "sample"
I try with various way
UIFont *font = [UIFont fontWithName:#"sample font" size:14];
UIFont *font = [UIFont fontWithName:#"sample" size:14];
UIFont *font = [UIFont fontWithName:#"samplefont" size:14];
UIFont *font = [UIFont fontWithName:#"samplefont-Bold" size:14];
but not working. what exact font name should have to given. ios is 5.0 .Plz help me.
Just double click the font file and install it and then it will open Font book
go to Preview menu->>Show font info
There you can see the name of the font and use that name in UIFont *font = [UIFont fontWithName:#"nameAsSeenInShowFonts" size:14];
Note: The font file name and font name can be different. So in your .plist you use font file name and in your code you use font name
if you try in Ios5 I think you forgot to include your font file in TestApp target membershipI
Except all these, you can use an alternet option for custom fonts. I have implemented this concept jst before some days.
First download this. This is FontLabel. Drop it in your project.
Note that If u want to use fonts for label than only this will help u. If so, than u can use FontLabel object instead of ur label object with same behavior.
For exa.
FontLabel *label;
label = [[FontLabel alloc] initWithFrame:CGRectMake(75, 100, 104, 54) fontName:#"Script MT Bold" pointSize:30.0f];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = nil;
[label sizeToFit];
label.opaque = NO;
You can treat label object same as ur UILabel object. FontLabel is subclass of UILabel..
I have a UILabel which has text containing both chinese and english characters,
now I want to set a font for chinese and another font for english,
how to do this?
There are couple of things that might be of interesting to you:
OHAttributedLabel
and TTTAttributedLabel
OHAttributedLabel stays it is capable of dealing with mixed fonts, color, size, ...
Generally one label can have only one font. Still if you want to show different font for different languages than you can keep different language string in different labels and arrange them the way you want.
See this to decide size of your labels.
Resize UITableViewCell to UILabel's height dynamically
and this is also helpful.
How do I wrap text in a UITableViewCell without a custom cell
I do not believe this is possible. The font property set in a UILabel would apply to the entire string specified in the text property of that UILabel.
I've not tried using chinese font, but you can use the following code to set different / multiple fonts & other properties on Label using NSMutableAttributedString. Foll is my code:
UIFont *ArialFont = [UIFont fontWithName:#"arial" size:18.0];
NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];
UIFont *VerdanaFont = [UIFont fontWithName:#"verdana" size:12.0];
NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];
[VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
[AattrString appendAttributedString:VattrString];
lblText.attributedText = AattrString;
Note that lblText is the UILabel, outlet as file owner.
One can keep on appending as many NSMutableAttributedString he wants..
Also Note that I've added verdana & arial font in my project & added a plist for the same.
When i use this line
[UIFont fontWithName:#"Arial" size:16] its work fine,but
[UIFont fontWithName:#"Arial Black" size:16] it crashes my app.....
In xib design there is the font name of Arial Black is available.But when i set using [UIFont fontWithName:#"Arial Black" size:16] this it crashes my app.. How can i set this font name to uilabel...
Can any one help me?
Check the return value of +[UIFont fontWithName:size:]. My guess is that "Arial Black" is not being found, and a nil font is being returned. Setting a UILabel's font to nil will crash.
To add - using +familyNames and +fontNamesForFamilyName methods in UIFont you can list all fonts available.
You need Arial-BoldMT
[UIFont fontWithName:#"Arial-BoldMT" size:16];
See here