react-native-navigation的迁移库

RNNFontAttributesCreator.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #import "RNNFontAttributesCreator.h"
  2. #import "RCTConvert+UIFontWeight.h"
  3. #define DEFAULT_FONT_SIZE 17.0f
  4. @implementation RNNFontAttributesCreator
  5. + (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
  6. NSMutableDictionary* titleTextAttributes = [NSMutableDictionary new];
  7. return [self createFromDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:defaultFontSize fontWeight:fontWeight color:color defaultColor:defaultColor];
  8. }
  9. + (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  10. NSMutableDictionary* titleTextAttributes = [NSMutableDictionary new];
  11. return [self createFromDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
  12. }
  13. + (NSDictionary *)createFromDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
  14. return [self createFromDictionary:attributesDictionary fontFamily:fontFamily fontSize:fontSize ?: defaultFontSize fontWeight:fontWeight color:color ?: defaultColor];
  15. }
  16. + (NSDictionary *)createFromDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
  17. NSMutableDictionary* titleTextAttributes = [NSMutableDictionary dictionaryWithDictionary:attributesDictionary];
  18. UIFont* currentFont = attributesDictionary[NSFontAttributeName];
  19. CGFloat resolvedFontSize = [self resolveFontSize:currentFont fontSize:fontSize];
  20. titleTextAttributes[NSForegroundColorAttributeName] = color;
  21. if (fontWeight) {
  22. titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize weight:[RCTConvert UIFontWeight:fontWeight]];
  23. } else if (fontFamily){
  24. titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:resolvedFontSize];
  25. } else if (fontSize && currentFont) {
  26. titleTextAttributes[NSFontAttributeName] = [UIFont fontWithDescriptor:currentFont.fontDescriptor size:resolvedFontSize];
  27. } else if (fontSize) {
  28. titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize];
  29. }
  30. return titleTextAttributes;
  31. }
  32. + (CGFloat)resolveFontSize:(UIFont *)currentFont fontSize:(NSNumber *)fontSize {
  33. if (fontSize) {
  34. return fontSize.floatValue;
  35. } else if (currentFont) {
  36. return currentFont.fontDescriptor.pointSize;
  37. } else {
  38. return DEFAULT_FONT_SIZE;
  39. }
  40. }
  41. @end