JDStatusBarView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // JDStatusBarView.m
  3. // JDStatusBarNotificationExample
  4. //
  5. // Created by Markus on 04.12.13.
  6. // Copyright (c) 2013 Markus. All rights reserved.
  7. //
  8. #import "JDStatusBarView.h"
  9. #import <sys/utsname.h>
  10. CGFloat const ADJUSTIPHONEX = 28;
  11. @interface JDStatusBarView ()
  12. @property (nonatomic, strong) UILabel *textLabel;
  13. @property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
  14. @end
  15. @implementation JDStatusBarView
  16. #pragma mark dynamic getter
  17. - (UILabel *)textLabel;
  18. {
  19. if (_textLabel == nil) {
  20. _textLabel = [[UILabel alloc] init];
  21. _textLabel.backgroundColor = [UIColor clearColor];
  22. _textLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
  23. _textLabel.textAlignment = NSTextAlignmentCenter;
  24. _textLabel.adjustsFontSizeToFitWidth = YES;
  25. _textLabel.clipsToBounds = YES;
  26. [self addSubview:_textLabel];
  27. }
  28. return _textLabel;
  29. }
  30. - (UIActivityIndicatorView *)activityIndicatorView;
  31. {
  32. if (_activityIndicatorView == nil) {
  33. _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  34. _activityIndicatorView.transform = CGAffineTransformMakeScale(0.7, 0.7);
  35. [self addSubview:_activityIndicatorView];
  36. }
  37. return _activityIndicatorView;
  38. }
  39. #pragma mark setter
  40. - (void)setTextVerticalPositionAdjustment:(CGFloat)textVerticalPositionAdjustment;
  41. {
  42. _textVerticalPositionAdjustment = textVerticalPositionAdjustment;
  43. [self setNeedsLayout];
  44. }
  45. #pragma mark layout
  46. - (void)layoutSubviews;
  47. {
  48. [super layoutSubviews];
  49. CGFloat yPos = self.textVerticalPositionAdjustment;
  50. CGFloat textLabelHeight = self.bounds.size.height-1;
  51. if ([JDStatusBarView isIphoneX]) {
  52. yPos += ADJUSTIPHONEX;
  53. textLabelHeight -= ADJUSTIPHONEX;
  54. }
  55. // label
  56. self.textLabel.frame = CGRectMake(0, 1+yPos,
  57. self.bounds.size.width, textLabelHeight);
  58. // activity indicator
  59. if (_activityIndicatorView ) {
  60. CGSize textSize = [self currentTextSize];
  61. CGRect indicatorFrame = _activityIndicatorView.frame;
  62. indicatorFrame.origin.x = round((self.bounds.size.width - textSize.width)/2.0) - indicatorFrame.size.width - 8.0;
  63. indicatorFrame.origin.y = ceil(1+(self.bounds.size.height - indicatorFrame.size.height)/2.0);
  64. if ([JDStatusBarView isIphoneX]) {
  65. indicatorFrame.origin.y += ADJUSTIPHONEX * 0.5;
  66. }
  67. _activityIndicatorView.frame = indicatorFrame;
  68. }
  69. }
  70. - (CGSize)currentTextSize;
  71. {
  72. CGSize textSize = CGSizeZero;
  73. // use new sizeWithAttributes: if possible
  74. SEL selector = NSSelectorFromString(@"sizeWithAttributes:");
  75. if ([self.textLabel.text respondsToSelector:selector]) {
  76. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
  77. NSDictionary *attributes = @{NSFontAttributeName:self.textLabel.font};
  78. textSize = [self.textLabel.text sizeWithAttributes:attributes];
  79. #endif
  80. }
  81. // otherwise use old sizeWithFont:
  82. else {
  83. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000 // only when deployment target is < ios7
  84. textSize = [self.textLabel.text sizeWithFont:self.textLabel.font];
  85. #endif
  86. }
  87. return textSize;
  88. }
  89. + (BOOL)isIphoneX
  90. {
  91. NSString *modelName = [self modelName];
  92. return [modelName isEqualToString: @"Phone10,3"] || [modelName isEqualToString: @"iPhone10,6"] || [JDStatusBarView isSimulatorIphoneX];
  93. }
  94. + (NSString *)modelName
  95. {
  96. struct utsname systemInfo;
  97. uname(&systemInfo);
  98. return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
  99. }
  100. + (BOOL)isSimulatorIphoneX
  101. {
  102. if ([JDStatusBarView isSimulator] && [UIScreen mainScreen].bounds.size.height == 812) {
  103. return YES;
  104. } else {
  105. return NO;
  106. }
  107. }
  108. + (BOOL)isSimulator
  109. {
  110. #if TARGET_OS_SIMULATOR
  111. return YES;
  112. #else
  113. return NO;
  114. #endif
  115. }
  116. @end