MoreView.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "MoreView.h"
  21. #import "BundleUtil.h"
  22. #import "UIDefines.h"
  23. #import "RectUtil.h"
  24. #import "UIImage+ColoredImage.h"
  25. #define X_OFFSET 36.0
  26. #define Y_OFFSET_MESSAGE 28.0
  27. #define HEIGHT_TITLE 22.0
  28. #define HEIGHT_MESSAGE 320.0
  29. #define X_PADDING 10.0
  30. #define FONT_SMALL [UIFont systemFontOfSize:14]
  31. #define FONT_TALL [UIFont systemFontOfSize:16]
  32. @interface MoreView ()
  33. @property CGRect originalRect;
  34. @property BOOL isShown;
  35. @property UIImageView *iconView;
  36. @property UILabel *moreLabel;
  37. @property UITextView *messageLabel;
  38. @end
  39. @implementation MoreView
  40. @synthesize moreButtonTitle = _moreButtonTitle;
  41. - (void)awakeFromNib {
  42. [super awakeFromNib];
  43. [self setup];
  44. }
  45. - (instancetype)initWithFrame:(CGRect)frame
  46. {
  47. self = [super initWithFrame:frame];
  48. if (self) {
  49. [self setup];
  50. }
  51. return self;
  52. }
  53. - (void)setup {
  54. #if !TARGET_INTERFACE_BUILDER
  55. UIImage *iconImage = [BundleUtil imageNamed:@"InfoFilled"];
  56. _iconView = [[UIImageView alloc] initWithImage:iconImage];
  57. _iconView.frame = CGRectMake(4.0, 2.0, 22.0, 22.0);
  58. [self addSubview:_iconView];
  59. #endif
  60. CGFloat labelWidth = self.frame.size.width - X_OFFSET;
  61. CGRect rect = CGRectMake(X_OFFSET, 2.0, labelWidth, HEIGHT_TITLE);
  62. _moreLabel = [[UILabel alloc] initWithFrame:rect];
  63. _moreLabel.font = FONT_SMALL;
  64. _moreLabel.textColor = [Colors mainThemeDark];
  65. _moreLabel.lineBreakMode = NSLineBreakByClipping;
  66. _moreLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  67. _moreLabel.text = [BundleUtil localizedStringForKey:@"more_information"];
  68. _moreLabel.backgroundColor = [UIColor clearColor];
  69. _moreLabel.accessibilityTraits = UIAccessibilityTraitButton;
  70. [self addSubview:_moreLabel];
  71. CGRect rectMessage = CGRectMake(X_OFFSET - 4.0, Y_OFFSET_MESSAGE, labelWidth, HEIGHT_MESSAGE);
  72. _messageLabel = [[UITextView alloc] initWithFrame:rectMessage];
  73. _messageLabel.font = FONT_TALL;
  74. _messageLabel.textColor = [UIColor whiteColor];
  75. _messageLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  76. _messageLabel.hidden = YES;
  77. _messageLabel.backgroundColor = [UIColor clearColor];
  78. _messageLabel.editable = NO;
  79. [self addSubview:_messageLabel];
  80. CGFloat y = CGRectGetMaxY(rectMessage) + 8.0;
  81. CGRect buttonRect = CGRectMake(0.0, y, 120.0, 36.0);
  82. _okButton = [[UIButton alloc] initWithFrame:buttonRect];
  83. _okButton.layer.cornerRadius = 3;
  84. _okButton.backgroundColor = [Colors mainThemeDark];
  85. _okButton.titleLabel.font = FONT_SMALL;
  86. [_okButton setTitleColor:[Colors white] forState:UIControlStateNormal];
  87. [_okButton setTitle:[BundleUtil localizedStringForKey:@"ok"] forState:UIControlStateNormal];
  88. _okButton.hidden = YES;
  89. [self addSubview:_okButton];
  90. _centerMoreView = NO;
  91. }
  92. - (void)setMoreButtonTitle:(NSString *)moreButtonTitle {
  93. _moreButtonTitle = moreButtonTitle;
  94. _moreLabel.text = _moreButtonTitle;
  95. }
  96. - (void)showMoreView {
  97. self.userInteractionEnabled = NO;
  98. _messageLabel.text = _moreMessageText;
  99. _originalRect = self.frame;
  100. _messageLabel.hidden = NO;
  101. _messageLabel.alpha = 0.0;
  102. CGRect buttonRect = [RectUtil rect:_okButton.frame centerHorizontalIn:self.frame round:YES];
  103. _okButton.frame = buttonRect;
  104. CGRect rect = [RectUtil offsetAndResizeRect:self.frame byX:0 byY:-(CGRectGetMinY(_okButton.frame))];
  105. CGFloat width = CGRectGetMaxX(_mainView.frame) - rect.origin.x - 2 * X_PADDING;
  106. rect = [RectUtil setWidthOf:rect width:width];
  107. if (_centerMoreView) {
  108. rect = CGRectMake((self.window.frame.size.width - rect.size.width)/2, (_mainView.frame.size.height - rect.size.height)/2, rect.size.width, rect.size.height);
  109. _okButton.frame = CGRectMake((rect.size.width - buttonRect.size.width)/2, buttonRect.origin.y, buttonRect.size.width, buttonRect.size.height);
  110. } else {
  111. _okButton.frame = CGRectMake(((rect.size.width - buttonRect.size.width) / 2) + X_PADDING, buttonRect.origin.y, buttonRect.size.width, buttonRect.size.height);
  112. }
  113. UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
  114. [UIView animateWithDuration:0.3 delay:0.0 options:options animations:^{
  115. self.frame = rect;
  116. _mainView.alpha = 0.0;
  117. _messageLabel.alpha = 1.0;
  118. _moreLabel.font = FONT_TALL;
  119. [_messageLabel setContentOffset:CGPointZero animated:NO];
  120. } completion:^(BOOL finished) {
  121. _okButton.hidden = NO;
  122. _mainView.hidden = YES;
  123. _isShown = YES;
  124. self.userInteractionEnabled = YES;
  125. UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.moreMessageText);
  126. }];
  127. }
  128. - (void)hideMoreView {
  129. self.userInteractionEnabled = NO;
  130. _mainView.alpha = 0.0;
  131. _mainView.hidden = NO;
  132. _okButton.hidden = YES;
  133. UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
  134. [UIView animateWithDuration:0.3 delay:0.0 options:options animations:^{
  135. self.frame = _originalRect;
  136. _mainView.alpha = 1.0;
  137. _messageLabel.alpha = 0.0;
  138. _moreLabel.font = FONT_SMALL;
  139. } completion:^(BOOL finished) {
  140. _messageLabel.hidden = YES;
  141. _mainView.hidden = NO;
  142. _isShown = NO;
  143. self.userInteractionEnabled = YES;
  144. }];
  145. }
  146. - (void)toggle {
  147. if (_isShown) {
  148. [self hideMoreView];
  149. } else {
  150. [self showMoreView];
  151. }
  152. }
  153. @end