JDStatusBarView.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. @interface JDStatusBarView ()
  10. @property (nonatomic, strong) UILabel *textLabel;
  11. @property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
  12. @end
  13. @implementation JDStatusBarView
  14. #pragma mark dynamic getter
  15. - (UILabel *)textLabel;
  16. {
  17. if (_textLabel == nil) {
  18. _textLabel = [[UILabel alloc] init];
  19. _textLabel.backgroundColor = [UIColor clearColor];
  20. _textLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
  21. _textLabel.textAlignment = NSTextAlignmentCenter;
  22. _textLabel.adjustsFontSizeToFitWidth = YES;
  23. _textLabel.clipsToBounds = YES;
  24. [self addSubview:_textLabel];
  25. }
  26. return _textLabel;
  27. }
  28. - (UIActivityIndicatorView *)activityIndicatorView;
  29. {
  30. if (_activityIndicatorView == nil) {
  31. _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  32. _activityIndicatorView.transform = CGAffineTransformMakeScale(0.7, 0.7);
  33. [self addSubview:_activityIndicatorView];
  34. }
  35. return _activityIndicatorView;
  36. }
  37. #pragma mark setter
  38. - (void)setTextVerticalPositionAdjustment:(CGFloat)textVerticalPositionAdjustment;
  39. {
  40. _textVerticalPositionAdjustment = textVerticalPositionAdjustment;
  41. [self setNeedsLayout];
  42. }
  43. #pragma mark layout
  44. - (void)layoutSubviews;
  45. {
  46. [super layoutSubviews];
  47. // label
  48. self.textLabel.frame = CGRectMake(0, 1+self.textVerticalPositionAdjustment,
  49. self.bounds.size.width, self.bounds.size.height-1);
  50. // activity indicator
  51. if (_activityIndicatorView ) {
  52. CGSize textSize = [self currentTextSize];
  53. CGRect indicatorFrame = _activityIndicatorView.frame;
  54. indicatorFrame.origin.x = round((self.bounds.size.width - textSize.width)/2.0) - indicatorFrame.size.width - 8.0;
  55. indicatorFrame.origin.y = ceil(1+(self.bounds.size.height - indicatorFrame.size.height)/2.0);
  56. _activityIndicatorView.frame = indicatorFrame;
  57. }
  58. }
  59. - (CGSize)currentTextSize;
  60. {
  61. CGSize textSize = CGSizeZero;
  62. // use new sizeWithAttributes: if possible
  63. SEL selector = NSSelectorFromString(@"sizeWithAttributes:");
  64. if ([self.textLabel.text respondsToSelector:selector]) {
  65. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
  66. NSDictionary *attributes = @{NSFontAttributeName:self.textLabel.font};
  67. textSize = [self.textLabel.text sizeWithAttributes:attributes];
  68. #endif
  69. }
  70. // otherwise use old sizeWithFont:
  71. else {
  72. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000 // only when deployment target is < ios7
  73. textSize = [self.textLabel.text sizeWithFont:self.textLabel.font];
  74. #endif
  75. }
  76. return textSize;
  77. }
  78. @end