我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?
当前回答
我是这样做的:
加载字体:
- (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),但它是有效的。
另外,你必须确保字体是合法嵌入的。大多数人都不是,有律师专门处理这类事情,所以要小心。
其他回答
目前还没有发布,但下一个版本的cocos2d (2d游戏框架)将支持可变长度的位图字体作为角色地图。
http://code.google.com/p/cocos2d-iphone/issues/detail?id=317
作者没有确定这个版本的发布日期,但我确实看到一个帖子说它将在未来一两个月发布。
在查找器和“获取信息”中找到TTF。在“全名:”的标题下,它给了我一个名字,然后我用fontWithName(我只是复制和粘贴了确切的名字,在这种情况下,没有'.ttf'扩展是必要的)。
是的,你可以在你的应用程序中使用自定义字体
按照下面的步骤:
将自定义字体文件添加到项目的支持文件中 为你的信息添加一个键。名为UIAppFonts的plist文件。 将该键设置为数组 对于你拥有的每种字体,输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项 保存Info.plist 现在在你的应用程序中,你可以简单地调用[UIFont fontWithName:@"你的自定义字体名称"大小:20]来获得与你的UILabels一起使用的自定义字体
应用这个后,如果你没有得到正确的字体 然后双击自定义字体,仔细看顶部字体的名字是来的 然后复制这个字体,粘贴到这里[UIFont fontWithName:@" here past your Custom fonts Name" size:20] 我希望你能得到正确答案
我是这样做的:
加载字体:
- (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),但它是有效的。
另外,你必须确保字体是合法嵌入的。大多数人都不是,有律师专门处理这类事情,所以要小心。
作为一个增强@bdev的答案,这里是一个更新版本列出自定义字体。
步骤1:使用@bdev的答案找出所有系统字体并保存到文件中。
将以下代码放在第一个视图控制器的-(void)viewDidLoad中,在[super viewDidLoad]之后(或在App Delegate中):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSMutableArray *system_fonts = [NSMutableArray array];
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
[system_fonts addObject:fontName];
}
}
if([paths count] > 0) {
[system_fonts writeToFile:[[paths objectAtIndex:0]
stringByAppendingPathComponent:@"array.out"] atomically:YES];
}
运行应用程序一次。事后停止。
步骤2:添加自定义字体到项目
使用已接受的答案中显示的方法,添加您的自定义字体(记得更新.plist并通过勾选add to Target添加要构建的字体文件。
步骤3:将系统字体与当前字体列表进行比较
将步骤1中的代码替换为:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSMutableArray *system_fonts = [NSMutableArray arrayWithContentsOfFile:[[paths objectAtIndex:0]
stringByAppendingPathComponent:@"array.out"]];
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
if (![system_fonts containsObject:fontName]) {
NSLog(@"%@", fontName);
}
}
}
运行App,你添加的自定义字体列表就会显示出来。
这适用于iOS 3.2到iOS 6(未来的版本可能会正常工作)。也适用于.ttc和.ttf。
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue