NewNotifierBar.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // NewNotifierBar.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/26.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "NewNotifierBar.h"
  9. #import <Accelerate/Accelerate.h>
  10. #import "NewNotifier.h"
  11. @interface NewNotifierBar ()
  12. @property (nonatomic, strong) UIImageView *iconView;
  13. @property (nonatomic, strong) UILabel *nameLabel;
  14. @property (nonatomic, strong) UILabel *detailLabel;
  15. @property (nonatomic, assign) UIEdgeInsets edge;
  16. @property (nonatomic, strong) UIFont *font;
  17. @end
  18. @implementation NewNotifierBar
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self)
  23. {
  24. [self buildWindow];
  25. }
  26. return self;
  27. }
  28. - (void)buildWindow
  29. {
  30. self.windowLevel = UIWindowLevelStatusBar + 1.0;
  31. self.hidden = NO;
  32. self.autoresizesSubviews = YES;
  33. self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.800];
  34. UIApplication *sharedApplication = [UIApplication sharedApplication];
  35. self.frame = sharedApplication.statusBarFrame;
  36. [self addSubview:self.iconView];
  37. [self addSubview:self.nameLabel];
  38. [self addSubview:self.detailLabel];
  39. }
  40. - (UIFont *)font{
  41. if (!_font) {
  42. _font =[UIFont systemFontOfSize:14.0];
  43. }
  44. return _font;
  45. }
  46. - (UIEdgeInsets)edge{
  47. return UIEdgeInsetsMake(8.0, 50.0, 20.0, 5.0);
  48. }
  49. - (UIImageView *)iconView;
  50. {
  51. if (!_iconView) {
  52. _iconView = [[UIImageView alloc] initWithFrame:self.bounds];
  53. _iconView.clipsToBounds = YES;
  54. }
  55. return _iconView;
  56. }
  57. //- (UILabel *)timeLabel;
  58. //{
  59. // if (!_timeLabel) {
  60. // _timeLabel = [[UILabel alloc] initWithFrame:self.bounds];
  61. // _timeLabel.textColor = [UIColor colorWithWhite:1.000 alpha:0.490];
  62. // _timeLabel.backgroundColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:0.000];
  63. // _timeLabel.font = [UIFont systemFontOfSize:NOTIFIER_FONT_SIZE-2];
  64. // _timeLabel.textAlignment = NSTextAlignmentLeft;
  65. // _timeLabel.clipsToBounds = YES;
  66. // }
  67. // return _timeLabel;
  68. //}
  69. - (UILabel *)nameLabel;
  70. {
  71. if (!_nameLabel) {
  72. _nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
  73. _nameLabel.textColor = [UIColor whiteColor];
  74. _nameLabel.backgroundColor = [UIColor clearColor];
  75. _nameLabel.font = self.font;
  76. _nameLabel.textAlignment = NSTextAlignmentJustified;
  77. _nameLabel.clipsToBounds = YES;
  78. _nameLabel.numberOfLines = 2;
  79. }
  80. return _nameLabel;
  81. }
  82. - (UILabel *)detailLabel;
  83. {
  84. if (!_detailLabel) {
  85. _detailLabel = [[UILabel alloc] initWithFrame:self.bounds];
  86. _detailLabel.textColor = [UIColor whiteColor];
  87. _detailLabel.backgroundColor = [UIColor clearColor];
  88. _detailLabel.font = self.font;
  89. _detailLabel.textAlignment = NSTextAlignmentJustified;
  90. _detailLabel.clipsToBounds = YES;
  91. _detailLabel.numberOfLines = 0;
  92. }
  93. return _detailLabel;
  94. }
  95. #pragma --mark getter
  96. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  97. if (_notifierBarClickBlock) {
  98. _notifierBarClickBlock(self.nameLabel.text?:@"",self.detailLabel.text?:@"",[NewNotifier shareInstance]);
  99. }
  100. }
  101. - (void)handleClickAction:(NewNotifierBarClickBlock)notifierBarClickBlock{
  102. _notifierBarClickBlock = [notifierBarClickBlock copy];
  103. }
  104. - (void)drawRect:(CGRect)rect{
  105. UIBezierPath *round = [UIBezierPath bezierPathWithRoundedRect:CGRectMake((CGRectGetWidth(self.frame)-35)/2, CGRectGetHeight(self.frame)-12, 35, 5) byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(10, 10)];
  106. [[UIColor lightGrayColor] setFill];
  107. [round fill];
  108. }
  109. - (void)show:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
  110. self.nameLabel.text = appName;
  111. self.detailLabel.text = note;
  112. self.iconView.image = appIcon;
  113. // self.timeLabel.text = @"刚刚";
  114. self.iconView.frame = CGRectMake(15, 7, 20, 20);
  115. CGFloat nameLabelHeight = MIN(40, [self heightWithString:appName fontSize:self.font.pointSize width:CGRectGetWidth(self.frame)-self.edge.left-self.edge.right]);
  116. self.nameLabel.frame = CGRectMake(self.edge.left, self.edge.top, CGRectGetWidth(self.frame)-self.edge.left-self.edge.right,nameLabelHeight);
  117. CGFloat detailLabelHeight = MIN(CGRectGetHeight([UIScreen mainScreen].bounds)-40-self.edge.bottom, [self heightWithString:note fontSize:self.font.pointSize width:CGRectGetWidth(self.frame)-self.edge.left-self.edge.right]);
  118. self.detailLabel.frame = CGRectMake(self.edge.left,
  119. CGRectGetMaxY(self.nameLabel.frame),
  120. CGRectGetWidth(self.frame)-self.edge.left-self.edge.right,detailLabelHeight);
  121. CGFloat selfHeight = MIN(CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetMaxY(self.detailLabel.frame)+self.edge.bottom);
  122. self.frame = CGRectMake(0,-selfHeight,CGRectGetWidth(self.frame),selfHeight);
  123. [self setNeedsDisplay];
  124. }
  125. - (CGFloat)widthWithString:(NSString*)string fontSize:(CGFloat)fontSize height:(CGFloat)height
  126. {
  127. NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
  128. return [string boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.width;
  129. }
  130. - (CGFloat)heightWithString:(NSString*)string fontSize:(CGFloat)fontSize width:(CGFloat)width
  131. {
  132. NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
  133. return [string boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.height;
  134. }
  135. @end