react-native-navigation的迁移库

RNNFontAttributesCreator.m 2.9KB

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. if (color) {
  21. titleTextAttributes[NSForegroundColorAttributeName] = color;
  22. }
  23. if (fontWeight) {
  24. titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize weight:[RCTConvert UIFontWeight:fontWeight]];
  25. } else if (fontFamily){
  26. titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:resolvedFontSize];
  27. } else if (fontSize && currentFont) {
  28. titleTextAttributes[NSFontAttributeName] = [UIFont fontWithDescriptor:currentFont.fontDescriptor size:resolvedFontSize];
  29. } else if (fontSize) {
  30. titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize];
  31. }
  32. return titleTextAttributes;
  33. }
  34. + (CGFloat)resolveFontSize:(UIFont *)currentFont fontSize:(NSNumber *)fontSize {
  35. if (fontSize) {
  36. return fontSize.floatValue;
  37. } else if (currentFont) {
  38. return currentFont.fontDescriptor.pointSize;
  39. } else {
  40. return DEFAULT_FONT_SIZE;
  41. }
  42. }
  43. @end