我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?
当前回答
Swift,代码方式:(也适用于Swift 2.0)
添加所需的字体到你的项目(就像添加图像,只需拖动到Xcode),确保他们是针对你的项目 添加这个方法并加载自定义字体(推荐在appDelegate didFinishLaunchingWithOptions中)
func loadFont(filePath: String) {
let fontData = NSData(contentsOfFile: filePath)!
let dataProvider = CGDataProviderCreateWithCFData(fontData)
let cgFont = CGFontCreateWithDataProvider(dataProvider)!
var error: Unmanaged<CFError>?
if !CTFontManagerRegisterGraphicsFont(cgFont, &error) {
let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
print("Unable to load font: %@", errorDescription, terminator: "")
}
}
使用的例子:
if let fontPath = NSBundle.mainBundle().pathForResource("My-Font", ofType: "ttf"){
loadFont(fontPath)
}
使用字体:
UIFont(name: "My-Font", size: 16.5)
其他回答
在信息。plist添加“Fonts provided by application”条目,并以字符串的形式包含字体名称:
Fonts provided by application
Item 0 myfontname.ttf
Item 1 myfontname-bold.ttf
...
然后运行以下命令,确保包含了你的字体:
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"%@", fontName);
}
}
请注意,您的ttf文件名可能与您设置标签字体时使用的名称不同(您可以使用上面的代码来获得"fontWithName"参数):
[label setFont:[UIFont fontWithName:@"MyFontName-Regular" size:18]];
在iOS 4中有一种使用自定义字体的简单方法。
将您的字体文件(例如,chalkdust .ttf)添加到XCode项目的资源文件夹中。 开放的信息。plist并添加一个名为UIAppFonts的新键。这个键的类型应该是array。 将您的自定义字体名称添加到该数组,包括扩展名(chalkdust .ttf)。 现在你可以在你的应用程序中使用[UIFont fontWithName:@"Chalkduster" size:16]。
不幸的是,IB不允许使用自定义字体初始化标签。看这个问题来解决这个问题。我最喜欢的解决方案是使用自定义UILabel子类:
@implementation CustomFontLabel
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder: decoder])
{
[self setFont: [UIFont fontWithName: @"Chalkduster" size: self.font.pointSize]];
}
return self;
}
@end
是的,您可以包括自定义字体。参考UIFont的文档,特别是fontWithName:size:方法。
1)确保在你的资源文件夹中包含了该字体。
2)字体的“名称”不一定是文件名。
3)确保你有使用这种字体的合法权利。通过把它包含在你的应用中,你也在传播它,你需要有这样做的权利。
我是这样做的:
加载字体:
- (void)loadFont{
// Get the path to our custom font and create a data provider.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"mycustomfont" ofType:@"ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
// Create the font with the data provider, then release the data provider.
customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
}
现在,在你的drawRect:中,像这样做:
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
// Get the context.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
// Set the customFont to be the font used to draw.
CGContextSetFont(context, customFont);
// Set how the context draws the font, what color, how big.
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetFillColorWithColor(context, self.fontColor.CGColor);
UIColor * strokeColor = [UIColor blackColor];
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
CGContextSetFontSize(context, 48.0f);
// Create an array of Glyph's the size of text that will be drawn.
CGGlyph textToPrint[[self.theText length]];
// Loop through the entire length of the text.
for (int i = 0; i < [self.theText length]; ++i) {
// Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
textToPrint[i] = [[self.theText uppercaseString] characterAtIndex:i] + 3 - 32;
}
CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, textTransform);
CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, [self.theText length]);
}
基本上,你必须在文本中进行一些暴力循环,并摆弄神奇的数字来找到字体中的偏移量(这里,看到我使用29),但它是有效的。
另外,你必须确保字体是合法嵌入的。大多数人都不是,有律师专门处理这类事情,所以要小心。
首先将.odt格式的字体添加到资源中,在本例中我们将使用DINEngschriftStd。Otf,然后使用这段代码将字体分配给标签
[theUILabel setFont:[UIFont fontWithName:@"DINEngschriftStd" size:21]];
要确保你的字体被加载到项目中,只需调用
NSLog(@"Available Font Families: %@", [UIFont familyNames]);
在.plist中,必须声明字体。只需添加一个'由应用程序提供的字体'记录,并添加一个带有字体名称的item 0字符串(DINEngschriftStd.otf)
推荐文章
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID
- 禁用所呈现视图控制器的交互式撤销
- 点击按钮时如何打开手机设置?
- 如何使用UIVisualEffectView来模糊图像?
- 如何修复UITableView分隔符在iOS 7?
- 故事板中的自定义单元格行高设置没有响应