刚好在做本地字体的功能。顺便记录下获取工程文件的方式。
1.添加文件到你的工程中
直接拉文件,到你的工程中。记住如下勾选。否则获取的路径永远是null。

2.代码获取路径
//添加在工程文件最外层使用方式
NSString *path = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"后缀"];
//下图1位置获取方式
NSString *path = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"ttf"];
//若你加在了某个文件夹中 依然使用如下方法!!!!!
NSString *path = [[NSBundle mainBundle] pathForResource:@"font2" ofType:@"ttf"];


注意!!!! 看上图中的2个文件夹颜色。只有在文件夹颜色为蓝色时,使用到:
NSString *path = [[NSBundle mainBundle] pathForResource:@"font1" ofType:@"ttf" inDirectory:@"Resources"];
而font2 虽然被文件夹font包含,但是依然要使用
NSString *path = [[NSBundle mainBundle] pathForResource:@"font2" ofType:@"ttf"];
如果你用 pathForResource:inDirectory:返回为null.切记上面提示。注意文件夹颜色。(本人就踩过坑了!)
网友评论