在iOS UILabel上设置BOLD字体 [英] Setting BOLD font on iOS UILabel
问题描述
我已经使用Interface Builder为UILabel中的文本分配了大小为14的Helvetica自定义字体。
我在多个地方使用相同的标签,但在某些地方,我必须以粗体显示文本。
有什么方法可以指定使现有的字体变成粗体而不是再次创建整个UIFont?这是我现在做的:
myLabel.font = [UIFont fontWithName:@Helvetica Neuesize:14];
更新: >
从iOS 8开始,不再需要使用字体名称。请参阅使用 UIFontDescriptorSymbolicTraits
的新答案:此处和这里。
myLabel.font = [UIFont fontWithName:@Helvetica-Boldsize:14];如果你想以编程方式设置它,你必须在iOS中检查粗体是否支持,通常是粗体或斜体将有格式的字体名称加粗,字体名称,斜体字体,字体名称,BoldItalic。
现在,写一个大胆的功能
- (void)boldFontForLabel:(UILabel *)label {
UIFont * currentFont = label.font;
UIFont * newFont = [UIFont fontWithName:[NSString stringWithFormat:@%@ - Bold,currentFont.fontName] size:currentFont.pointSize];
label.font = newFont;
$ / code>
然后调用它
[self boldFontForLabel:yourLabel];
I have assigned a custom font of 'Helvetica' with size 14 already for the text in UILabel using Interface Builder.
I am using reusing the same label at multiple places, but at some place I have to display the text in bold.
Is there any way I can just specify to make the existing font bold instead of creating the whole UIFont again? This is what I do now:
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
解决方案 UPDATE:
Starting with iOS 8, messing with font names is no longer needed. Instead see newer answers that use UIFontDescriptorSymbolicTraits
: here and here.
myLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
If you wanna set it programmatically,you must check bold type is support or not in iOS, normally Bold or Italic will have format FontName-Bold, FontName-Italic, FontName-BoldItalic.
Now, write a bold function
-(void)boldFontForLabel:(UILabel *)label{
UIFont *currentFont = label.font;
UIFont *newFont = [UIFont fontWithName:[NSString stringWithFormat:@"%@-Bold",currentFont.fontName] size:currentFont.pointSize];
label.font = newFont;
}
Then call it
[self boldFontForLabel:yourLabel];
这篇关于在iOS UILabel上设置BOLD字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!