可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have following code in my Application.
tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.tag=1;
tmp.textColor=[UIColor blackColor];
[tmp setFont:[UIFont fontWithName:@"American Typewriter" size:18]];
tmp.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:tmp];
[tmp release];
Now, I need to know,
======> how to set "Typeface=bold" through code?
回答1:
You will need to use the name of the bold
font within the family. To find out if there is a bold
version of American Typewriter, try outputting
[UIFont fontNamesForFamilyName:@"AmericanTypewriter"]
to the console.
In this case, you should use "AmericanTypewriter-Bold"
.
[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"]
回答2:
What about using the setter property:
// Create a string
NSString *text = @"You are getting sleepy.";
// Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];
// Draw the string
[text drawInRect:(CGRect)
withFont:font];
回答3:
Just NSLog The font you want get:
NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);
And you get the array:
(
"AmericanTypewriter-CondensedLight",
"AmericanTypewriter-Light",
"AmericanTypewriter-Bold",
AmericanTypewriter,
"AmericanTypewriter-CondensedBold",
"AmericanTypewriter-Condensed"
)
Use any you need in Code:
UILabel * loading = [[UILabel alloc] initWithFrame:CGRectMake(350, 402, 150, 25)];
[loading setFont:[UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]];
[loading setTextColor:[UIColor whiteColor]];
[loading setBackgroundColor:[UIColor clearColor]];
[loading setText:@"Loading ..."];
[self.view addSubview:loading];
回答4:
You can get all the iOS supported font names in this site iOSfonts.
Take preview of your exact string to find the best font and use it like mentioned in above answers
UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 150, 50)];
[loading setFont:[UIFont fontWithName:@"Avenir-Black" size:12.0]];
[loading setTextColor:[UIColor redColor]];
[loading setBackgroundColor:[UIColor clearColor]];
[loading setText:@"My Label Title"];
[self.view myLabel];
回答5:
Like @devinfoley wrote, you have to add -Bold
to the fontName
you're using. For me it doesn't work with fontNamesForFamilyName
, instead I'm using fontWithName:size
. Maybe it works if you create the UILabel
programmatically. In my case I'm just setting the font
inside the extension of my UILabel
in awakeFromNib
and set this extension as class
in the Identity Inspector
for my specific UILabel
. With self.font.pointSize
you can set the fontSize
inside the Interface Builder
and can handle it better if you have more UI elements.
- (void)awakeFromNib{
[super awakeFromNib];
[self setAdjustsFontSizeToFitWidth:YES];
[self setFont:[UIFont fontWithName:@"Font-Bold" size:self.font.pointSize]];
}
If your font
doesn't work you should print all fonts
of the family, so you can see if you wrote the fontName
wrong. This way you also can check if there is a bold
font available, if its not printed you don't have this option.
NSLog (@"Available Fonts: %@", [UIFont fontNamesForFamilyName:@"Your Font"]);
More font
stuff: https://stackoverflow.com/a/8529661/1141395
回答6:
Swift update
If you've already added the font to the app (as explained here) you can use Swift's extension
feature with, as example, a UILabel
and the Roboto font:
extension UILabel {
func setFont(style: String, size: Int){
self.font = UIFont(name: style, size: CGFloat(size))
}
struct FontStyle {
public static let italic: String = "Roboto-LightItalic"
public static let normal: String = "Roboto-Light"
public static let thin: String = "Roboto-Thin"
}
}
Roboto-LightItalic
and Roboto-Light
are the TTF's font file names. Then, you can simply call
someUILabel.setFont(style: UILabel.FontStyle.normal, size: 20)