我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?
当前回答
在信息。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]];
其他回答
我是这样做的:
加载字体:
- (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),但它是有效的。
另外,你必须确保字体是合法嵌入的。大多数人都不是,有律师专门处理这类事情,所以要小心。
在查找器和“获取信息”中找到TTF。在“全名:”的标题下,它给了我一个名字,然后我用fontWithName(我只是复制和粘贴了确切的名字,在这种情况下,没有'.ttf'扩展是必要的)。
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)
对于iOS 3.2及以上版本: 使用上面几个提供的方法,它们是:
将您的字体文件(例如,chalkdust .ttf)添加到XCode项目的资源文件夹中。 开放的信息。plist并添加一个名为UIAppFonts的新键。这个键的类型应该是array。 将您的自定义字体名称添加到该数组,包括扩展名(" chalkdust .ttf")。 在你的应用中使用[UIFont fontWithName:@"真实字体名称"大小:16]。
但 “真正的字体名称”并不总是一个你看到的字体。最好的方法是问你的设备它看到的字体和确切的名称是什么。
我使用uifont-name-grabber发布在: uifont-name-grabber
只需将你想要的字体放到xcode项目中,将文件名添加到它的plist中,并在你要构建的设备上运行它,它会通过电子邮件给你一个完整的字体列表,使用UIFont fontWithName:所期望的名称。
我做了一切可能,但新的字体不出现,所以我找到了解决方案:
当你拖动fot文件(otf或ttf)时,不要忘记勾选“添加到目标”下的复选框。
这样做之后,你的字体就会出现,一切都会正常工作。
推荐文章
- 我如何在网站上使用谷歌的Roboto字体?
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何确定浏览器渲染文本时实际使用的字体?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示