react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #import "RCTHelpers.h"
  2. #import <React/RCTView.h>
  3. #import <React/RCTScrollView.h>
  4. #import <React/RCTFont.h>
  5. @implementation RCTHelpers
  6. + (NSMutableDictionary *)textAttributesFromDictionary:(NSDictionary *)dictionary withPrefix:(NSString *)prefix baseFont:(UIFont *)baseFont
  7. {
  8. NSMutableDictionary *textAttributes = [NSMutableDictionary new];
  9. NSString *colorKey = @"color";
  10. NSString *familyKey = @"fontFamily";
  11. NSString *weightKey = @"fontWeight";
  12. NSString *sizeKey = @"fontSize";
  13. NSString *styleKey = @"fontStyle";
  14. NSString *shadowColourKey = @"shadowColor";
  15. NSString *shadowOffsetKey = @"shadowOffset";
  16. NSString *shadowBlurRadiusKey = @"shadowBlurRadius";
  17. NSString *showShadowKey = @"showShadow";
  18. if (prefix) {
  19. colorKey = [colorKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[colorKey substringToIndex:1].capitalizedString];
  20. colorKey = [NSString stringWithFormat:@"%@%@", prefix, colorKey];
  21. familyKey = [familyKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[familyKey substringToIndex:1].capitalizedString];
  22. familyKey = [NSString stringWithFormat:@"%@%@", prefix, familyKey];
  23. weightKey = [weightKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[weightKey substringToIndex:1].capitalizedString];
  24. weightKey = [NSString stringWithFormat:@"%@%@", prefix, weightKey];
  25. sizeKey = [sizeKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[sizeKey substringToIndex:1].capitalizedString];
  26. sizeKey = [NSString stringWithFormat:@"%@%@", prefix, sizeKey];
  27. styleKey = [styleKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[styleKey substringToIndex:1].capitalizedString];
  28. styleKey = [NSString stringWithFormat:@"%@%@", prefix, styleKey];
  29. shadowColourKey = [shadowColourKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowColourKey substringToIndex:1].capitalizedString];
  30. shadowColourKey = [NSString stringWithFormat:@"%@%@", prefix, shadowColourKey];
  31. shadowOffsetKey = [shadowOffsetKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowOffsetKey substringToIndex:1].capitalizedString];
  32. shadowOffsetKey = [NSString stringWithFormat:@"%@%@", prefix, shadowOffsetKey];
  33. shadowBlurRadiusKey = [shadowBlurRadiusKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowBlurRadiusKey substringToIndex:1].capitalizedString];
  34. shadowBlurRadiusKey = [NSString stringWithFormat:@"%@%@", prefix, shadowBlurRadiusKey];
  35. showShadowKey = [showShadowKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[showShadowKey substringToIndex:1].capitalizedString];
  36. showShadowKey = [NSString stringWithFormat:@"%@%@", prefix, showShadowKey];
  37. }
  38. NSShadow *shadow;
  39. NSNumber *shadowColor = dictionary[shadowColourKey];
  40. if (shadowColor && [shadowColor isKindOfClass:[NSNumber class]]) {
  41. if (!shadow) {
  42. shadow = [NSShadow new];
  43. }
  44. shadow.shadowColor = [RCTConvert UIColor:shadowColor];
  45. }
  46. NSDictionary *shadowOffsetDict = dictionary[shadowOffsetKey];
  47. if (shadowOffsetDict && [shadowOffsetDict isKindOfClass:[NSDictionary class]]) {
  48. CGSize shadowOffset = [RCTConvert CGSize:shadowOffsetDict];
  49. if (!shadow) {
  50. shadow = [NSShadow new];
  51. }
  52. shadow.shadowOffset = shadowOffset;
  53. }
  54. NSNumber *shadowRadius = dictionary[shadowBlurRadiusKey];
  55. if (shadowRadius) {
  56. CGFloat radius = [RCTConvert CGFloat:shadowRadius];
  57. if (!shadow) {
  58. shadow = [NSShadow new];
  59. }
  60. shadow.shadowBlurRadius = radius;
  61. }
  62. NSNumber *showShadow = dictionary[showShadowKey];
  63. if (showShadow) {
  64. BOOL show = [RCTConvert BOOL:showShadow];
  65. if (!show) {
  66. shadow = nil;
  67. }
  68. }
  69. if (shadow) {
  70. [textAttributes setObject:shadow forKey:NSShadowAttributeName];
  71. }
  72. NSNumber *textColor = dictionary[colorKey];
  73. if (textColor && [textColor isKindOfClass:[NSNumber class]])
  74. {
  75. UIColor *color = [RCTConvert UIColor:textColor];
  76. [textAttributes setObject:color forKey:NSForegroundColorAttributeName];
  77. }
  78. NSString *fontFamily = dictionary[familyKey];
  79. if (![fontFamily isKindOfClass:[NSString class]]) {
  80. fontFamily = nil;
  81. }
  82. NSString *fontWeight = dictionary[weightKey];
  83. if (![fontWeight isKindOfClass:[NSString class]]) {
  84. fontWeight = nil;
  85. }
  86. NSNumber *fontSize = dictionary[sizeKey];
  87. if (![fontSize isKindOfClass:[NSNumber class]]) {
  88. fontSize = nil;
  89. }
  90. NSString *fontStyle = dictionary[styleKey];
  91. if (![fontStyle isKindOfClass:[NSString class]]) {
  92. fontStyle = nil;
  93. }
  94. UIFont *font = [RCTFont updateFont:baseFont withFamily:fontFamily size:fontSize weight:fontWeight style:fontStyle variant:nil scaleMultiplier:1];
  95. if (font && (fontStyle || fontWeight || fontSize || fontFamily)) {
  96. [textAttributes setObject:font forKey:NSFontAttributeName];
  97. }
  98. return textAttributes;
  99. }
  100. + (NSMutableDictionary *)textAttributesFromDictionary:(NSDictionary *)dictionary withPrefix:(NSString *)prefix
  101. {
  102. return [self textAttributesFromDictionary:dictionary withPrefix:prefix baseFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
  103. }
  104. + (NSString *)getTimestampString {
  105. long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
  106. return [NSString stringWithFormat:@"%lld", milliseconds];
  107. }
  108. @end