ProgressLabel.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ProgressLabel.h"
  21. #import "RectUtil.h"
  22. #import "BundleUtil.h"
  23. #import "UIImage+ColoredImage.h"
  24. #define ACTIVITY_INDICATOR_PADDING 6.0
  25. #define NUMBER_OF_LINES 2
  26. @interface ProgressLabel ()
  27. @property UIActivityIndicatorView *activityIndicatior;
  28. @property UILabel *label;
  29. @property UIImageView *statusView;
  30. @end
  31. @implementation ProgressLabel
  32. - (void)awakeFromNib {
  33. [super awakeFromNib];
  34. [self setup];
  35. }
  36. - (instancetype)initWithFrame:(CGRect)frame
  37. {
  38. self = [super initWithFrame:frame];
  39. if (self) {
  40. [self setup];
  41. }
  42. return self;
  43. }
  44. - (void)setup {
  45. _activityIndicatior = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  46. _activityIndicatior.hidden = YES;
  47. _activityIndicatior.frame = [RectUtil rect:_activityIndicatior.frame centerVerticalIn:self.bounds round:YES];
  48. [self addSubview:_activityIndicatior];
  49. _label = [[UILabel alloc] initWithFrame:self.bounds];
  50. _label.numberOfLines = NUMBER_OF_LINES;
  51. [self addSubview:_label];
  52. }
  53. - (void)setText:(NSString *)text {
  54. [_label setText:text];
  55. }
  56. - (NSString *)text {
  57. return _label.text;
  58. }
  59. - (void)hideActivityIndicator {
  60. _label.frame = self.bounds;
  61. [_activityIndicatior stopAnimating];
  62. _activityIndicatior.alpha = 0.0;
  63. }
  64. - (void)showActivityIndicator {
  65. CGFloat maxX = CGRectGetMaxX(_activityIndicatior.frame) + ACTIVITY_INDICATOR_PADDING;
  66. CGRect labelFrame = [RectUtil offsetAndResizeRect:self.bounds byX:maxX byY:0.0];
  67. _label.frame = labelFrame;
  68. [_activityIndicatior startAnimating];
  69. _activityIndicatior.alpha = 1.0;
  70. }
  71. - (void)showErrorMessage:(NSString *)errorMessage {
  72. [self setStatusImage:@"ExclamationMark"];
  73. _label.text = errorMessage;
  74. }
  75. - (void)showSuccessMessage:(NSString *)successMessage {
  76. [self setStatusImage:@"Checkbox"];
  77. _label.text = successMessage;
  78. }
  79. - (void)setStatusImage:(NSString *)imageName {
  80. UIImage *image = [BundleUtil imageNamed:imageName];
  81. if ([imageName isEqualToString:@"Checkbox"]) {
  82. image = [image imageWithTint:[UIColor whiteColor]];
  83. }
  84. if (_statusView == nil) {
  85. _statusView = [[UIImageView alloc] initWithImage:image];
  86. _statusView.frame = [RectUtil growRect:_activityIndicatior.frame byDx:-6.0 byDy:-6.0];
  87. [self addSubview:_statusView];
  88. } else {
  89. _statusView.image = image;
  90. }
  91. if (CGRectIntersectsRect(_label.frame, _statusView.frame)) {
  92. CGFloat maxX = CGRectGetMaxX(_statusView.frame) + ACTIVITY_INDICATOR_PADDING;
  93. CGRect labelFrame = [RectUtil offsetAndResizeRect:self.bounds byX:maxX byY:0.0];
  94. _label.frame = labelFrame;
  95. }
  96. _activityIndicatior.hidden = YES;
  97. }
  98. - (void)setFont:(UIFont *)font {
  99. _label.font = font;
  100. }
  101. - (UIFont *)font {
  102. return _label.font;
  103. }
  104. - (void)setTextColor:(UIColor *)textColor {
  105. _label.textColor = textColor;
  106. }
  107. - (UIColor *)textColor {
  108. return _label.textColor;
  109. }
  110. - (void)setNumberOfLines:(NSInteger)numberOfLines {
  111. _label.numberOfLines = numberOfLines;
  112. }
  113. - (NSInteger)numberOfLines {
  114. return _label.numberOfLines;
  115. }
  116. @end