MASExampleLabelView.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // MASExampleLabelView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Jonas Budelmann on 24/10/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleLabelView.h"
  9. static UIEdgeInsets const kPadding = {10, 10, 10, 10};
  10. @interface MASExampleLabelView ()
  11. @property (nonatomic, strong) UIButton *bigButton;
  12. @property (nonatomic, strong) UILabel *shortLabel;
  13. @property (nonatomic, strong) UILabel *longLabel;
  14. @end
  15. @implementation MASExampleLabelView
  16. - (id)init {
  17. self = [super init];
  18. if (!self) return nil;
  19. // text courtesy of http://baconipsum.com/
  20. self.bigButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  21. [self.bigButton setTitle:@"Big Button" forState:UIControlStateNormal];
  22. [self.bigButton setContentEdgeInsets:UIEdgeInsetsMake(20, 5, 20, 5)];
  23. self.bigButton.layer.borderColor = UIColor.blueColor.CGColor;
  24. self.bigButton.layer.borderWidth = 1;
  25. [self addSubview:self.bigButton];
  26. self.shortLabel = UILabel.new;
  27. self.shortLabel.numberOfLines = 3;
  28. self.shortLabel.textColor = [UIColor purpleColor];
  29. self.shortLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  30. self.shortLabel.text = @"Bacon ipsum dolor sit amet spare ribs fatback kielbasa salami, tri-tip jowl pastrami flank short loin rump sirloin. Tenderloin frankfurter chicken biltong rump chuck filet mignon pork t-bone flank ham hock.";
  31. [self addSubview:self.shortLabel];
  32. self.longLabel = UILabel.new;
  33. self.longLabel.numberOfLines = 8;
  34. self.longLabel.textColor = [UIColor darkGrayColor];
  35. self.longLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  36. self.longLabel.text = @"Bacon ipsum dolor sit amet spare ribs fatback kielbasa salami, tri-tip jowl pastrami flank short loin rump sirloin. Tenderloin frankfurter chicken biltong rump chuck filet mignon pork t-bone flank ham hock.";
  37. [self addSubview:self.longLabel];
  38. [self.bigButton mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.equalTo(self).insets(kPadding);
  40. make.right.equalTo(self).insets(kPadding);
  41. make.width.equalTo(@100);
  42. }];
  43. [self.shortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.top.equalTo(self).insets(kPadding);
  45. make.left.equalTo(self).insets(kPadding);
  46. make.right.equalTo(self.bigButton.mas_left).offset(-5);
  47. }];
  48. [self.longLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.top.equalTo(self.shortLabel.mas_bottom).offset(kPadding.bottom);
  50. make.left.equalTo(self).insets(kPadding);
  51. make.right.equalTo(self).insets(kPadding);
  52. }];
  53. return self;
  54. }
  55. - (void)layoutSubviews {
  56. [super layoutSubviews];
  57. // for multiline UILabel's you need set the preferredMaxLayoutWidth
  58. // you need to do this after [super layoutSubviews] as the frames will have a value from Auto Layout at this point
  59. // stay tuned for new easier way todo this coming soon to Masonry
  60. self.shortLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.shortLabel.frame);
  61. self.longLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.longLabel.frame);
  62. // need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth
  63. [super layoutSubviews];
  64. }
  65. @end