UIScreen+YYAdd.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // UIScreen+YYAdd.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/5.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "UIScreen+YYAdd.h"
  12. #import "YYKitMacro.h"
  13. #import "UIDevice+YYAdd.h"
  14. YYSYNTH_DUMMY_CLASS(UIScreen_YYAdd);
  15. @implementation UIScreen (YYAdd)
  16. + (CGFloat)screenScale {
  17. static CGFloat screenScale = 0.0;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. if ([NSThread isMainThread]) {
  21. screenScale = [[UIScreen mainScreen] scale];
  22. } else {
  23. dispatch_sync(dispatch_get_main_queue(), ^{
  24. screenScale = [[UIScreen mainScreen] scale];
  25. });
  26. }
  27. });
  28. return screenScale;
  29. }
  30. - (CGRect)currentBounds {
  31. return [self boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
  32. }
  33. - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation {
  34. CGRect bounds = [self bounds];
  35. if (UIInterfaceOrientationIsLandscape(orientation)) {
  36. CGFloat buffer = bounds.size.width;
  37. bounds.size.width = bounds.size.height;
  38. bounds.size.height = buffer;
  39. }
  40. return bounds;
  41. }
  42. - (CGSize)sizeInPixel {
  43. CGSize size = CGSizeZero;
  44. if ([[UIScreen mainScreen] isEqual:self]) {
  45. NSString *model = [UIDevice currentDevice].machineModel;
  46. if ([model hasPrefix:@"iPhone"]) {
  47. if ([model isEqualToString:@"iPhone7,1"]) return CGSizeMake(1080, 1920);
  48. if ([model isEqualToString:@"iPhone8,2"]) return CGSizeMake(1080, 1920);
  49. if ([model isEqualToString:@"iPhone9,2"]) return CGSizeMake(1080, 1920);
  50. if ([model isEqualToString:@"iPhone9,4"]) return CGSizeMake(1080, 1920);
  51. }
  52. if ([model hasPrefix:@"iPad"]) {
  53. if ([model hasPrefix:@"iPad6,7"]) size = CGSizeMake(2048, 2732);
  54. if ([model hasPrefix:@"iPad6,8"]) size = CGSizeMake(2048, 2732);
  55. }
  56. }
  57. if (CGSizeEqualToSize(size, CGSizeZero)) {
  58. if ([self respondsToSelector:@selector(nativeBounds)]) {
  59. size = self.nativeBounds.size;
  60. } else {
  61. size = self.bounds.size;
  62. size.width *= self.scale;
  63. size.height *= self.scale;
  64. }
  65. if (size.height < size.width) {
  66. CGFloat tmp = size.height;
  67. size.height = size.width;
  68. size.width = tmp;
  69. }
  70. }
  71. return size;
  72. }
  73. - (CGFloat)pixelsPerInch {
  74. if (![[UIScreen mainScreen] isEqual:self]) {
  75. return 326;
  76. }
  77. static CGFloat ppi = 0;
  78. static dispatch_once_t one;
  79. static NSString *name;
  80. dispatch_once(&one, ^{
  81. NSDictionary<NSString*, NSNumber *> *dic = @{
  82. @"Watch1,1" : @326, //@"Apple Watch 38mm",
  83. @"Watch1,2" : @326, //@"Apple Watch 43mm",
  84. @"Watch2,3" : @326, //@"Apple Watch Series 2 38mm",
  85. @"Watch2,4" : @326, //@"Apple Watch Series 2 42mm",
  86. @"Watch2,6" : @326, //@"Apple Watch Series 1 38mm",
  87. @"Watch1,7" : @326, //@"Apple Watch Series 1 42mm",
  88. @"iPod1,1" : @163, //@"iPod touch 1",
  89. @"iPod2,1" : @163, //@"iPod touch 2",
  90. @"iPod3,1" : @163, //@"iPod touch 3",
  91. @"iPod4,1" : @326, //@"iPod touch 4",
  92. @"iPod5,1" : @326, //@"iPod touch 5",
  93. @"iPod7,1" : @326, //@"iPod touch 6",
  94. @"iPhone1,1" : @163, //@"iPhone 1G",
  95. @"iPhone1,2" : @163, //@"iPhone 3G",
  96. @"iPhone2,1" : @163, //@"iPhone 3GS",
  97. @"iPhone3,1" : @326, //@"iPhone 4 (GSM)",
  98. @"iPhone3,2" : @326, //@"iPhone 4",
  99. @"iPhone3,3" : @326, //@"iPhone 4 (CDMA)",
  100. @"iPhone4,1" : @326, //@"iPhone 4S",
  101. @"iPhone5,1" : @326, //@"iPhone 5",
  102. @"iPhone5,2" : @326, //@"iPhone 5",
  103. @"iPhone5,3" : @326, //@"iPhone 5c",
  104. @"iPhone5,4" : @326, //@"iPhone 5c",
  105. @"iPhone6,1" : @326, //@"iPhone 5s",
  106. @"iPhone6,2" : @326, //@"iPhone 5s",
  107. @"iPhone7,1" : @401, //@"iPhone 6 Plus",
  108. @"iPhone7,2" : @326, //@"iPhone 6",
  109. @"iPhone8,1" : @326, //@"iPhone 6s",
  110. @"iPhone8,2" : @401, //@"iPhone 6s Plus",
  111. @"iPhone8,4" : @326, //@"iPhone SE",
  112. @"iPhone9,1" : @326, //@"iPhone 7",
  113. @"iPhone9,2" : @401, //@"iPhone 7 Plus",
  114. @"iPhone9,3" : @326, //@"iPhone 7",
  115. @"iPhone9,4" : @401, //@"iPhone 7 Plus",
  116. @"iPad1,1" : @132, //@"iPad 1",
  117. @"iPad2,1" : @132, //@"iPad 2 (WiFi)",
  118. @"iPad2,2" : @132, //@"iPad 2 (GSM)",
  119. @"iPad2,3" : @132, //@"iPad 2 (CDMA)",
  120. @"iPad2,4" : @132, //@"iPad 2",
  121. @"iPad2,5" : @264, //@"iPad mini 1",
  122. @"iPad2,6" : @264, //@"iPad mini 1",
  123. @"iPad2,7" : @264, //@"iPad mini 1",
  124. @"iPad3,1" : @324, //@"iPad 3 (WiFi)",
  125. @"iPad3,2" : @324, //@"iPad 3 (4G)",
  126. @"iPad3,3" : @324, //@"iPad 3 (4G)",
  127. @"iPad3,4" : @324, //@"iPad 4",
  128. @"iPad3,5" : @324, //@"iPad 4",
  129. @"iPad3,6" : @324, //@"iPad 4",
  130. @"iPad4,1" : @324, //@"iPad Air",
  131. @"iPad4,2" : @324, //@"iPad Air",
  132. @"iPad4,3" : @324, //@"iPad Air",
  133. @"iPad4,4" : @264, //@"iPad mini 2",
  134. @"iPad4,5" : @264, //@"iPad mini 2",
  135. @"iPad4,6" : @264, //@"iPad mini 2",
  136. @"iPad4,7" : @264, //@"iPad mini 3",
  137. @"iPad4,8" : @264, //@"iPad mini 3",
  138. @"iPad4,9" : @264, //@"iPad mini 3",
  139. @"iPad5,1" : @264, //@"iPad mini 4",
  140. @"iPad5,2" : @264, //@"iPad mini 4",
  141. @"iPad5,3" : @324, //@"iPad Air 2",
  142. @"iPad5,4" : @324, //@"iPad Air 2",
  143. @"iPad6,3" : @324, //@"iPad Pro (9.7 inch)",
  144. @"iPad6,4" : @324, //@"iPad Pro (9.7 inch)",
  145. @"iPad6,7" : @264, //@"iPad Pro (12.9 inch)",
  146. @"iPad6,8" : @264, //@"iPad Pro (12.9 inch)",
  147. };
  148. NSString *model = [UIDevice currentDevice].machineModel;
  149. if (model) {
  150. ppi = dic[name].doubleValue;
  151. }
  152. if (ppi == 0) ppi = 326;
  153. });
  154. return ppi;
  155. }
  156. @end