123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //
- // JDStatusBarView.m
- // JDStatusBarNotificationExample
- //
- // Created by Markus on 04.12.13.
- // Copyright (c) 2013 Markus. All rights reserved.
- //
- #import "JDStatusBarView.h"
- #import <sys/utsname.h>
- CGFloat const ADJUSTIPHONEX = 28;
- @interface JDStatusBarView ()
- @property (nonatomic, strong) UILabel *textLabel;
- @property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
- @end
- @implementation JDStatusBarView
- #pragma mark dynamic getter
- - (UILabel *)textLabel;
- {
- if (_textLabel == nil) {
- _textLabel = [[UILabel alloc] init];
- _textLabel.backgroundColor = [UIColor clearColor];
- _textLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
- _textLabel.textAlignment = NSTextAlignmentCenter;
- _textLabel.adjustsFontSizeToFitWidth = YES;
- _textLabel.clipsToBounds = YES;
- [self addSubview:_textLabel];
- }
- return _textLabel;
- }
- - (UIActivityIndicatorView *)activityIndicatorView;
- {
- if (_activityIndicatorView == nil) {
- _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
- _activityIndicatorView.transform = CGAffineTransformMakeScale(0.7, 0.7);
- [self addSubview:_activityIndicatorView];
- }
- return _activityIndicatorView;
- }
- #pragma mark setter
- - (void)setTextVerticalPositionAdjustment:(CGFloat)textVerticalPositionAdjustment;
- {
- _textVerticalPositionAdjustment = textVerticalPositionAdjustment;
- [self setNeedsLayout];
- }
- #pragma mark layout
- - (void)layoutSubviews;
- {
- [super layoutSubviews];
-
- CGFloat yPos = self.textVerticalPositionAdjustment;
- CGFloat textLabelHeight = self.bounds.size.height-1;
- if ([JDStatusBarView isIphoneX]) {
- yPos += ADJUSTIPHONEX;
- textLabelHeight -= ADJUSTIPHONEX;
- }
- // label
- self.textLabel.frame = CGRectMake(0, 1+yPos,
- self.bounds.size.width, textLabelHeight);
- // activity indicator
- if (_activityIndicatorView ) {
- CGSize textSize = [self currentTextSize];
- CGRect indicatorFrame = _activityIndicatorView.frame;
- indicatorFrame.origin.x = round((self.bounds.size.width - textSize.width)/2.0) - indicatorFrame.size.width - 8.0;
- indicatorFrame.origin.y = ceil(1+(self.bounds.size.height - indicatorFrame.size.height)/2.0);
-
- if ([JDStatusBarView isIphoneX]) {
- indicatorFrame.origin.y += ADJUSTIPHONEX * 0.5;
- }
-
- _activityIndicatorView.frame = indicatorFrame;
- }
- }
- - (CGSize)currentTextSize;
- {
- CGSize textSize = CGSizeZero;
- // use new sizeWithAttributes: if possible
- SEL selector = NSSelectorFromString(@"sizeWithAttributes:");
- if ([self.textLabel.text respondsToSelector:selector]) {
- #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
- NSDictionary *attributes = @{NSFontAttributeName:self.textLabel.font};
- textSize = [self.textLabel.text sizeWithAttributes:attributes];
- #endif
- }
- // otherwise use old sizeWithFont:
- else {
- #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000 // only when deployment target is < ios7
- textSize = [self.textLabel.text sizeWithFont:self.textLabel.font];
- #endif
- }
- return textSize;
- }
- + (BOOL)isIphoneX
- {
- NSString *modelName = [self modelName];
- return [modelName isEqualToString: @"Phone10,3"] || [modelName isEqualToString: @"iPhone10,6"] || [JDStatusBarView isSimulatorIphoneX];
- }
- + (NSString *)modelName
- {
- struct utsname systemInfo;
- uname(&systemInfo);
- return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
- }
- + (BOOL)isSimulatorIphoneX
- {
- if ([JDStatusBarView isSimulator] && [UIScreen mainScreen].bounds.size.height == 812) {
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)isSimulator
- {
- #if TARGET_OS_SIMULATOR
- return YES;
- #else
- return NO;
- #endif
- }
- @end
|