我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?
当前回答
遵循这个步骤
1)在项目中复制你的字体
2)以源代码模式打开你的.plist文件…(注意-不要打开info.plist)
3)在此之前-右键单击你的字体,在fontforge或类似的编辑器中打开它,并将其安装到你的系统中,它应该是安装的
4)类型
<key>UIAppFonts</key>
<array>
<string>MyriadPro.otf</string>
</array>
5)在你的类中输入这个代码。m
[lblPoints setFont:[UIFont fontWithName:@"Myriad Pro" size:15.0]];
这里lblPoints将改变你的UILabel
完成了! ! 如果你的字体仍然不能工作,首先检查你的字体兼容性
其他回答
这是如何做这件事的一步一步的说明。不需要额外的库或任何特殊的编码。
http://shang-liang.com/blog/custom-fonts-in-ios4/
大多数时候问题在于字体而不是方法。最好的方法是使用一种肯定有效的字体,比如verdana或geogia。然后更改为想要的字体。如果它不起作用,可能是字体名称不正确,或者字体格式不正确。
是的,您可以包括自定义字体。参考UIFont的文档,特别是fontWithName:size:方法。
1)确保在你的资源文件夹中包含了该字体。
2)字体的“名称”不一定是文件名。
3)确保你有使用这种字体的合法权利。通过把它包含在你的应用中,你也在传播它,你需要有这样做的权利。
iOS 3.2及后续版本支持此功能。直接摘自iPhone OS 3.2新增功能文档:
自定义字体支持 想要使用自定义字体的应用程序现在可以将这些字体包含在他们的应用程序包中,并通过在他们的信息中包含UIAppFonts键向系统注册这些字体。plist文件。该键的值是一个字符串数组,用于标识应用程序bundle中的字体文件。当系统看到键时,它加载指定的字体并使它们对应用程序可用。
一旦字体设置在信息。plist,您可以使用您的自定义字体作为任何其他字体在IB或编程。
苹果开发者论坛上有一个正在进行的话题: https://devforums.apple.com/thread/37824(需要登录)
这里有一个优秀而简单的三步教程,教你如何做到这一点(断开的链接被删除)
将您的自定义字体文件添加到您的项目中,使用Xcode作为资源 为你的信息添加一个键。名为UIAppFonts的plist文件。 将该键设置为数组 对于你拥有的每种字体,输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项 保存Info.plist 现在在你的应用程序中,你可以简单地调用[UIFont fontWithName:@"CustomFontName" size:12]来获得自定义字体来使用你的UILabels和UITextViews等…
另外:确保字体在复制包资源中。
我是这样做的:
加载字体:
- (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),但它是有效的。
另外,你必须确保字体是合法嵌入的。大多数人都不是,有律师专门处理这类事情,所以要小心。
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)
推荐文章
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 新的自动引用计数机制是如何工作的?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像