EaseBubbleView+Location.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseBubbleView+Location.h"
  13. @implementation EaseBubbleView (Location)
  14. - (void)layoutSubviews {
  15. [super layoutSubviews];
  16. //
  17. // CGFloat availableLabelWidth = self.frame.size.width;
  18. // self.locationLabel.preferredMaxLayoutWidth = availableLabelWidth - 30;
  19. //
  20. // [super layoutSubviews];
  21. }
  22. #pragma mark - private
  23. - (void)_setupLocationBubbleMarginConstraints
  24. {
  25. NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.locationImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
  26. NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.locationImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
  27. NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.locationImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
  28. NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.locationImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
  29. [self.marginConstraints removeAllObjects];
  30. [self.marginConstraints addObject:marginTopConstraint];
  31. [self.marginConstraints addObject:marginBottomConstraint];
  32. [self.marginConstraints addObject:marginLeftConstraint];
  33. [self.marginConstraints addObject:marginRightConstraint];
  34. [self addConstraints:self.marginConstraints];
  35. }
  36. - (void)_setupLocationBubbleConstraints
  37. {
  38. [self _setupLocationBubbleMarginConstraints];
  39. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.locationImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
  40. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.locationImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
  41. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.locationImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
  42. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.locationImageView attribute:NSLayoutAttributeHeight multiplier:0.3 constant:0]];
  43. }
  44. #pragma mark - public
  45. - (void)setupLocationBubbleView
  46. {
  47. self.locationImageView = [[UIImageView alloc] init];
  48. self.locationImageView.translatesAutoresizingMaskIntoConstraints = NO;
  49. self.locationImageView.backgroundColor = [UIColor clearColor];
  50. self.locationImageView.clipsToBounds = YES;
  51. [self.backgroundImageView addSubview:self.locationImageView];
  52. self.locationLabel = [[UILabel alloc] init];
  53. self.locationLabel.translatesAutoresizingMaskIntoConstraints = NO;
  54. self.locationLabel.numberOfLines = 2;
  55. self.locationLabel.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.8];
  56. // self.locationLabel.preferredMaxLayoutWidth = 0.0f;
  57. [self.locationImageView addSubview:self.locationLabel];
  58. [self _setupLocationBubbleConstraints];
  59. }
  60. - (void)updateLocationMargin:(UIEdgeInsets)margin
  61. {
  62. if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
  63. return;
  64. }
  65. _margin = margin;
  66. [self removeConstraints:self.marginConstraints];
  67. [self _setupLocationBubbleMarginConstraints];
  68. }
  69. @end