IOS中自定义字体的粗体实现

来源:互联网 发布:淘宝上白酒好卖吗 编辑:程序博客网 时间:2024/05/13 06:09

加载自定义字体的方法网上很多,大概就是先把xx.ttf 或者xx.ttc等格式的字体文件导入xcode ,在info.plist里配置一下:添加key:Fonts provided by application 并将其赋值为xx.ttf;然后遍历一下UIFont的familyNames 找到你需要的对应的fontName。

自定义的字体弄好后,有时候需要用这个font的粗体。系统只提供了boldsystemFont。所以普通的绘制就不能用了,下面的函数可以实现自定义的粗体绘制


+ (UILabel *)labelWithFrame:(CGRect)frame WithBoldFont:(UIFont*)font strokeWidth:(float)width withAttributeText:(NSString*)string
{
NSAttributedString *atbString = [[NSAttributedString alloc]
initWithString:string
attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,
[NSNumber numberWithFloat:width],NSStrokeWidthAttributeName, nil]];
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.attributedText = atbString;
return label;
}