我正在为我的项目上的一个项目工作我想添加自定义字体,如Calibri Bold.ttf
和Calibri.ttf
为此我在我的应用程序和我添加的info.plist文件中添加了这个文件Fonts Provided by Aplication
然后它显示在我的字体中,但是当我想要设置我的字符串时它会显示我的错误,如
-(NSMutableAttributedString *)getAttributedString:(NSString*)lostr
{
NSMutableAttributedString *loMutAttStr = [[NSMutableAttributedString alloc]initWithString:lostr];
NSArray *loArr = [lostr componentsSeparatedByString:@"("];
NSRange loRange1 = NSMakeRange(0,[[loArr objectAtIndex:0] length]-1);
NSRange loRange2 = NSMakeRange([lostr length]-[[loArr objectAtIndex:[loArr count]-1] length]-1,[[loArr objectAtIndex:[loArr count]-1] length]+1);
UIColor *loColor = [UIColor blackColor];
UIColor *loColr1= [UIColor grayColor];
[loMutAttStr addAttribute:NSForegroundColorAttributeName value:loColor range:loRange1];
[loMutAttStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Calibri Bold" size:30] range:loRange1];
if([loArr count] > 1)
{
[loMutAttStr addAttribute:NSForegroundColorAttributeName value:loColr1 range:loRange2];
[loMutAttStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Calibri" size:30] range:loRange2];
}
return loMutAttStr;
}
然后它会在行
中显示错误NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString addAttribute:value:range:: nil value'
[loMutAttStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Calibri Bold" size:30] range:loRange1];
请在这里给我解决方案,我的字符串不是零,当我设置字体HelveticaNeue
而不是Calibri
时,它工作正常。
答案 0 :(得分:0)
点击这里添加自定义字体http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
打印app支持的字体
for (NSString* family in [UIFont familyNames])
{
NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(@" %@", name);
}
}