react-native-navigation的迁移库

RNNFontAttributesCreator.m 2.4KB

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