BallotMatrixLabelView.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-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 "BallotMatrixLabelView.h"
  21. #import "RectUtil.h"
  22. #define FONT_COLOR [Colors fontNormal]
  23. #define HAIRLINE_WIDTH 1.0f
  24. #define MAX_WIDTH 300.0f
  25. #define X_PADDING 4.0f
  26. @interface BallotMatrixLabelView ()
  27. @property UILabel *label;
  28. @end
  29. @implementation BallotMatrixLabelView
  30. + (instancetype)labelForString:(NSString *)text at:(CGRect)rect {
  31. BallotMatrixLabelView *view = [[BallotMatrixLabelView alloc] initWithFrame:rect];
  32. view.maxWidth = MAX_WIDTH;
  33. [view setupLabel];
  34. [view setText:text];
  35. return view;
  36. }
  37. - (void)setText:(NSString *)text {
  38. [_label setText:text];
  39. }
  40. - (NSString *)text {
  41. return _label.text;
  42. }
  43. -(void)setFont:(UIFont *)font {
  44. _label.font = font;
  45. }
  46. -(UIFont *)font {
  47. return _label.font;
  48. }
  49. - (void)setTextAlignment:(NSTextAlignment)textAlignment {
  50. _label.textAlignment = textAlignment;
  51. }
  52. - (void)setBorderColor: (UIColor *)color {
  53. self.layer.borderColor = color.CGColor;
  54. }
  55. - (void)setBorderWidth: (CGFloat)width {
  56. self.layer.borderWidth = width;
  57. }
  58. - (void)setTextColor:(UIColor *)color {
  59. _label.textColor = color;
  60. }
  61. - (void) setupLabel {
  62. CGRect labelRect = [RectUtil growRect:self.bounds byDx:-2.0*X_PADDING byDy:0.0];
  63. _label = [[UILabel alloc] initWithFrame:labelRect];
  64. _label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  65. _label.textColor = FONT_COLOR;
  66. [self addSubview:_label];
  67. }
  68. - (CGSize)sizeThatFits:(CGSize)size {
  69. CGSize labelSize = [_label sizeThatFits:size];
  70. CGFloat width = fminf(_maxWidth, labelSize.width);
  71. return CGSizeMake(width, self.bounds.size.height);
  72. }
  73. -(void)offsetAndResizeHeight:(CGFloat)yOffset {
  74. _label.frame = [RectUtil offsetAndResizeRect:_label.frame byX:0.0 byY:yOffset];
  75. }
  76. @end