Light's Blog

The best or nothing.

iOS知识小集-170515

| Comments

更新cocoapods版本

正式版:sudo gem install cocoapods
开发版:sudo gem install cocoapods --pre

Get local bundle

通过frameworkNamebundleName获取对应bundle。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+ (NSBundle *)getBundleWithFrameworkName:(NSString *)frameworkName bundleName:(NSString *)bundleName {
  NSString *tmpBundleName = [bundleName copy];
  if (![bundleName hasSuffix:@".bundle"]) {
    tmpBundleName = [NSString stringWithFormat:@"%@.bundle", tmpBundleName];
  }

  NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
  NSString *bundlePath = [mainBundlePath stringByAppendingPathComponent:tmpBundleName];
  NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
  if (bundle) {
    return bundle;
  }

  NSString *tempFramework = [frameworkName copy];
  NSString *frameExtension = @".framework";
  if (![tempFramework hasSuffix:frameExtension]) {
    tempFramework = [tempFramework stringByAppendingString:frameExtension];
  }

  NSString *path = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:tempFramework];
  return [NSBundle bundleWithPath:[path stringByAppendingPathComponent:tmpBundleName]];
}

Localized string in cocoapods

在自定义pod中添加语言本地化:
1. 为MyPod添加语言路径MyPod/Languages/**/*,pod中使用的不同语言的.strings存放在该目录下;
2. 在MyPod.podspec文件中添加语言bundle:s.resource_bundles = {'LanguageBundle' => ['MyPod/Languages/**/*']}
3. 添加简便调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
NSString *XGLocalizedString(NSString *key, NSString *comment) {
  return [XGFitnessLocalize localizedString:key];
}

+ (NSString *)localizedString:(NSString *)key {
  //  key == nil, return nil;
  if (!key) {
    return key;
  }
  //  system language
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSArray *languages = [defaults objectForKey:kAppleLanguageKey];
  NSString *language = [languages firstObject];
  //  set default language
  NSString *fileNamePrefix = language;
  if (!([fileNamePrefix isEqualToString:kLanguageEnglish] ||
        [fileNamePrefix isEqualToString:kLanguageSimplifiedChinese] ||
        [fileNamePrefix isEqualToString:kLanguageTraditionalChinese])) {
    fileNamePrefix = kDefaultLanguage;
  }
  //  language bundle
  NSBundle *languageBundle =
      [WWFitnessBundleUtil getBundleWithFrameworkName:kFrameworkName bundleName:kLanguageBundleName];
  //  lproj bundle
  NSString *path = [languageBundle pathForResource:fileNamePrefix ofType:@"lproj"];
  NSBundle *lprojBundle = [NSBundle bundleWithPath:path];
  //  localized string
  NSString *localizedString = [lprojBundle localizedStringForKey:key value:@"" table:@"Localizable"];
  if (!localizedString) {
    localizedString = key;
  }
  return localizedString;
}

Image in cocoapods

在自定义pod中添加图片步骤:
1. 为MyPod添加图片路径MyPod/Assets/Images,pod中使用的图片存放在该目录下;
2. 在MyPod.podspec文件中添加图片bundle:s.resource_bundles = {'ImageBundle' => ['MyPod/Assets/Images/**/*']}
3. 为UIImage添加类别方法bundleImageNamed:

1
2
3
4
5
6
7
8
@implementation UIImage (BundleImage)

+ (UIImage *)bundleImageNamed:(NSString *)name {
  NSBundle *imageBundle = [WWFitnessBundleUtil getBundleWithFrameworkName:@"MyPod" bundleName:@"ImageBundle"];
  return [UIImage imageNamed:name inBundle:imageBundle compatibleWithTraitCollection:nil];
}

@end

Font in cocoapods

自定义字体无法静态添加到自定义pod中,需要动态注册字体才能使用。
1. 为MyPod添加字体路径MyPod/Fonts,pod中使用的字体存放在该目录下;
2. 在MyPod.podspec文件中添加字体bundle:s.resource_bundles = {'FontBundle' => ['MyPod/Fonts/*']}
3. 创建注册字体方法,并在使用前仅调用一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#import <CoreText/CTFontManager.h>

+ (void)registerFitnessFont {
  [self registerFitnessFont:@"DIN-Regular"];
  [self registerFitnessFont:@"DIN-Medium"];
  [self registerFitnessFont:@"DIN-Bold"];
}

+ (void)registerFitnessFont:(NSString *)fontName {
  NSBundle *fontBundle = [WWFitnessBundleUtil getBundleWithFrameworkName:@"MyPod" bundleName:@"FontBundle"];
  NSURL *fontURL = [fontBundle URLForResource:fontName withExtension:@"otf" /*or TTF*/];
  NSData *inData = [NSData dataWithContentsOfURL:fontURL];
  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);
  }
  CFSafeRelease(font);
  CFSafeRelease(provider);
}

void CFSafeRelease(CFTypeRef cf) {
  if (cf != NULL) {
    CFRelease(cf);
  }
}

Comments