简体   繁体   English

如何在数组中仅加载自定义字体名称?

[英]How to load only Custom font names in Array?

I am using Multiple custom fonts in my app.i know how to Add custom font but the Terrible parts of all this process is when we are going to use the custom font because mostly the custom font name is different from the file that we are using in app.here i would like to explain the recent example that i did,First of all i have added two custom font file in my app 我在我的应用程序中使用了多种自定义字体。我知道如何添加自定义字体,但是所有此过程的糟糕之处是当我们要使用自定义字体时,因为大多数自定义字体名称与我们正在使用的文件不同在app.here中,我想解释一下我最近所做的示例,首先,我在我的应用程序中添加了两个自定义字体文件 在此处输入图片说明

For getting the above custom font names i tried this way 为了获得上述自定义字体名称,我尝试了这种方式

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];

NSArray *fontNames;
NSInteger indFamily, indFont;
 FontnameArray=[[NSMutableArray alloc] init];
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
   fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSString *fullName = [NSString stringWithFormat:@"%@",[fontNames objectAtIndex:indFont]];
        [FontnameArray addObject:fullName];
        }
    }
[FontnameArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"    Font name: %@", FontnameArray);

The above Code display 199 font names for me ,so it was to difficult for me to pick the custom font name among 199 fonts, To make my doubt clear then i have simply drag the font file to font book Then i got the orignal names of custom Font. 上面的代码为我显示了199个字体名称,因此我很难在199个字体中选择自定义字体名称,要明确我的疑问,我只是将字体文件拖到字体书中,然后得到了自定义字体。

[UIFont fontWithName:@"Ethiopia Primary" size:20]];
[UIFont fontWithName:@"Ethiopia jiret" size:20]];

So the Question is how can i get all Custom font names in Array like Above instead of draging the each font file to Fontbook for getting orignal names. 所以问题是我如何才能像上面一样在Array中获取所有自定义字体名称,而不是将每个字体文件拖到Fontbook以获得原始名称。

Just compare with all fonts exist in iOS 只需与iOS中存在的所有字体进行比较

*Create new project like this code and write array of all font names to a plist file *像这样的代码创建新项目,并将所有字体名称的数组写入plist文件

- (void)writeAlliOSFontsToPlist{
    NSArray *fontFamilies = [UIFont familyNames];
    [self savePlist:@"AllFont" fromArray:fontFamilies];
}

- (NSString *) getFilePathForPlist:(NSString *) plistName {
    // placeholder
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]];
}

-(void) savePlist:(NSString *)plistName fromArray:(NSArray *)array {
    [array writeToFile:[self getFilePathForPlist:plistName] atomically:YES];
}

Open your simulator dir and copy this plist to your App bundle. 打开您的模拟器目录,然后将此plist复制到您的App捆绑包中。 Then read this plist to an array and compare with all of your fonts by this code 然后将此plist读入数组,并通过此代码与所有字体进行比较

-(void)dumpCustomFonts{
    NSArray *iOSFontsArray = [NSArray arrayWithContentsOfFile:
                              [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
                               @"AllFont.plist"]];
    NSMutableArray *fontFamilies = (NSMutableArray *)[UIFont familyNames];
    [fontFamilies removeObjectsInArray:iOSFontsArray];
    NSLog(@"%@",fontFamilies);
}

I've done all in my test project, you just copy and paste it. 我已经在测试项目中完成了所有工作,您只需复制并粘贴即可。

You should add your custom font in .plist by 您应该在.plist添加自定义字体,方法是

1) Add a new entry with the key "Fonts provided by application" . 1)使用键"Fonts provided by application"添加一个新条目。

2) Now add each custom font file name with extension to this array like : 2)现在,将每个带有扩展名的自定义字体文件名添加到此数组,例如:

在此处输入图片说明

Now you can retrive all your custom fonts by : 现在,您可以通过以下方式检索所有自定义字体:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

fontFamilyNames = [plistData valueForKey:@"UIAppFonts"];
NSLog(@"%@",fontFamilyNames);

NSString *fontFamilyName = [[fontFamilyNames objectAtIndex:index] stringByDeletingPathExtension];
UILabel *fontNameLabel = [[[UILabel alloc] init] autorelease];        
fontNameLabel.text = @"Preview Text";
fontNameLabel.font = [UIFont fontWithName:fontFamilyName size:20.0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM