Ios 我可以在iPhone应用程序中嵌入自定义字体吗?
我想让一个应用程序包含一个用于呈现文本的自定义字体,加载它,然后将其与标准Ios 我可以在iPhone应用程序中嵌入自定义字体吗?,ios,cocoa-touch,fonts,Ios,Cocoa Touch,Fonts,我想让一个应用程序包含一个用于呈现文本的自定义字体,加载它,然后将其与标准UIKit元素一起使用,如UILabel。这可能吗?是的,您可以包括自定义字体。请参阅有关UIFont的文档,特别是fontWithName:size:方法 1) 确保在资源文件夹中包含该字体 2) 字体的“名称”不一定是文件名 3) 确保您拥有使用该字体的合法权利。通过将其包含在应用程序中,您也在分发它,您需要有权这样做。我这样做: -(void)drawRect:(CGRect)rect{ [super dra
UIKit
元素一起使用,如UILabel
。这可能吗?是的,您可以包括自定义字体。请参阅有关UIFont的文档,特别是fontWithName:size:
方法
1) 确保在资源文件夹中包含该字体
2) 字体的“名称”不一定是文件名
3) 确保您拥有使用该字体的合法权利。通过将其包含在应用程序中,您也在分发它,您需要有权这样做。我这样做:
-(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]);
}
加载字体:
- (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);
}
UIFont(name: "My-Font", size: 16.5)
现在,在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]);
}
-(void)drawRect:(CGRect)rect{
[超级drawRect:rect];
//获取上下文。
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextClearRect(上下文,rect);
//将customFont设置为用于绘制的字体。
CGContextSetFont(上下文,自定义字体);
//设置上下文绘制字体的方式、颜色和大小。
CGContextSetTextDrawingMode(上下文,kCGTextFillStroke);
CGContextSetFillColorWithColor(上下文,self.fontColor.CGColor);
UIColor*strokeColor=[UIColor blackColor];
CGContextSetStrokeColorWithColor(上下文,strokeColor.CGColor);
CGContextSetFontSize(上下文,48.0f);
//创建一个将要绘制的文本大小的字形数组。
cglyph textToPrint[[self.theText length]];
//循环浏览整个文本长度。
对于(int i=0;i<[self.theText length];++i){
//将每个字母存储在字形中,然后减去MagicNumber以获得适当的值。
textToPrint[i]=[[self.theText大写字符串]字符索引:i]+3-32;
}
CGAffineTransform textTransform=CGAffineTransformMake(1.0,0.0,0.0,-1.0,0.0,0.0);
CGContextSetTextMatrix(上下文,textTransform);
CGContextShowGlyphsAtPoint(上下文,20,50,textToPrint,[self.theText长度]);
}
基本上,你必须做一些蛮力循环,通过文本和神奇的数字来找到你的偏移量(这里,请看我使用29),但它的工作
此外,您必须确保字体是合法嵌入的。大多数人都不是,而且有专门从事这类工作的律师,所以请注意。请在应用程序Fontspath查阅
一个简单的plist条目,允许您将字体文件包含在应用程序资源文件夹中,它们在应用程序中“正常工作”。尚未发布,但下一版本的cocos2d(2d游戏框架)将支持变长位图字体作为角色映射
作者没有确定这个版本的发布日期,但我确实看到一则帖子,指出它将在下一两个月发布。编辑:从iOS 3.2开始,这个功能是内置的。如果您需要支持3.2之前的版本,您仍然可以使用此解决方案。
我创建了一个简单的模块,它扩展了UILabel
,并处理.ttf文件的加载。我在Apache许可下开源发布了它,并将其放在github上
重要的文件是FontLabel.h
和FontLabel.m
它使用了来自的一些代码
浏览源代码
或
- 将字体文件复制到资源中
- 在名为UIAppFonts的
Info.plist
文件中添加一个键。(“应用程序提供的字体)
- 将此键设置为数组
- 对于您拥有的每种字体,将字体文件(包括扩展名)的全名作为项目输入UIAppFonts数组
- 保存
Info.plist
- 现在,在应用程序中,您只需调用
[UIFont-fontWithName:@“CustomFontName”size:15]
即可获得自定义字体,以便与UILabels
和uitextview
等一起使用
编辑:此答案自iOS3.2起失效;请使用UIAppFonts
我能够成功加载自定义UIFont
s的唯一方法是通过private GraphicsServices框架
以下内容将加载应用程序主捆绑包中的所有.ttf
字体:
BOOL GSFontAddFromFile(const char * path);
NSUInteger loadFonts()
{
NSUInteger newFontCount = 0;
for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
return newFontCount;
}
一旦加载字体,它们就可以像苹果提供的字体一样使用:
NSLog(@"Available Font Families: %@", [UIFont familyNames]);
[label setFont:[UIFont fontWithName:@"Consolas" size:20.0f]];
GraphicsServices甚至可以在运行时加载,以防API在将来消失:
#import <dlfcn.h>
NSUInteger loadFonts()
{
NSUInteger newFontCount = 0;
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
if (frameworkPath) {
void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
if (graphicsServices) {
BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
if (GSFontAddFromFile)
for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
}
}
return newFontCount;
}
#导入
NSU整数加载字体()
{
NSU整数newFontCount=0;
NSBundle*frameworkBundle=[NSBundle BundleWithiIdentifier:@“com.apple.GraphicsServices”];
const char*frameworkPath=[[frameworkBundle executablePath]UTF8String];
if(框架路径){
void*graphicsServices=dlopen(框架路径,RTLD_-NOLOAD | RTLD_-LAZY);
if(图形服务){
BOOL(*GSFontAddFromFile)(const char*)=dlsym(graphicsServices,“GSFontAddFromFile”);
if(GSFontAddFromFile)
对于(位于[[NSBundle mainBundle]路径中的NSString*fontFile ForResourceSoftType:@“ttf”目录:nil])
newFontCount+=GSFontAddFromFile([fontFile UTF8String]);
}
}
返回newFontCount;
}
也许作者忘了给字体加字母了
在中打开字体,然后转到元素>字体信息
有一个“Mac”选项,您可以在其中设置喜欢的名称
在文件>导出字体下,可以创建新的ttf
您还可以尝试一下导出对话框中的“Apple”选项
免责声明:我不是IPhone开发者!我在iOS 3.1.2上尝试了本页面上的各种建议,以下是我的结论:
简单地将[UIFont-fontWithName:size::
与资源目录中的字体一起使用将不起作用,即使使用FontForge设置了字体名称
[UIFont fontWithName:size:][/code>如果先使用GSFon加载字体,则可以使用
[theUILabel setFont:[UIFont fontWithName:@"DINEngschriftStd" size:21]];
NSLog(@"Available Font Families: %@", [UIFont familyNames]);
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);
}
}
[label setFont:[UIFont fontWithName:@"MyFontName-Regular" size:18]];
<key>UIAppFonts</key>
<array>
<string>MyriadPro.otf</string>
</array>
[lblPoints setFont:[UIFont fontWithName:@"Myriad Pro" size:15.0]];
NSData *inData = /* your font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error)
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
[self.labelOutlet setFont:[UIFont fontWithName:@"Sathu" size:10]];
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description)
{
println(" \(name)")
}
}
}
for family: String in UIFont.familyNames(){
print("\(family)")
for names: String in UIFont.fontNamesForFamilyName(family){
print("== \(names)")
}
}
for (NSString* family in [UIFont familyNames]){
NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family]){
NSLog(@" %@", name);
}
}
label.font = UIFont(name: "SourceSansPro-Regular", size: 18)
label.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:18];
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)