CustomCalloutView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // CustomCalloutView.m
  3. // amap_location
  4. //
  5. // Created by i2edu on 2020/4/23.
  6. //
  7. #import "CustomCalloutView.h"
  8. #import "CustomAnnotation.h"
  9. #define kArrorHeight 10
  10. #define kPortraitMargin 5
  11. #define kTitleWidth 190
  12. #define kTitleHeight 20
  13. #define kTitleSize 12
  14. #define kSubTitleSize 12
  15. #define kPhoneNumSize 12
  16. #define kGotoSize 12
  17. @interface CustomCalloutView ()
  18. @property (nonatomic, strong) UILabel *subtitleLabel;
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. @property (nonatomic, strong) UILabel *phoneLabel;
  21. @property (nonatomic, strong) UILabel *clickLabel;
  22. @end
  23. @implementation CustomCalloutView
  24. - (id)initWithFrame:(CGRect)frame
  25. {
  26. self = [super initWithFrame:frame];
  27. if (self)
  28. {
  29. self.backgroundColor = [UIColor clearColor];
  30. [self initSubViews];
  31. }
  32. return self;
  33. }
  34. - (void)initSubViews
  35. {
  36. // 添加标题,即商户名
  37. self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 , kPortraitMargin, kTitleWidth, kTitleHeight)];
  38. self.titleLabel.font = [UIFont boldSystemFontOfSize:kTitleSize];
  39. self.titleLabel.textColor = [UIColor blackColor];
  40. self.titleLabel.text = _title;
  41. self.titleLabel.numberOfLines=0;
  42. [self addSubview:self.titleLabel];
  43. // 添加副标题,即商户地址
  44. self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];
  45. self.subtitleLabel.font = [UIFont systemFontOfSize:kSubTitleSize];
  46. self.subtitleLabel.textColor = [UIColor grayColor];
  47. self.subtitleLabel.text = [NSString stringWithFormat:@"地址:%@",_subtitle];
  48. self.subtitleLabel.numberOfLines=0;
  49. [self addSubview:self.subtitleLabel];
  50. // 店家d电话
  51. self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2, kPortraitMargin * 2 + kTitleHeight*2, kTitleWidth, kTitleHeight)];
  52. self.phoneLabel.font = [UIFont systemFontOfSize:kSubTitleSize];
  53. self.phoneLabel.textColor = [UIColor grayColor];
  54. self.phoneLabel.text = [NSString stringWithFormat:@"电话:%@",_phoneNumber];
  55. self.phoneLabel.numberOfLines=0;
  56. [self addSubview:self.phoneLabel];
  57. // 添加到这里去
  58. self.clickLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 , kPortraitMargin * 2 + kTitleHeight*3, kTitleWidth, kTitleHeight)];
  59. self.clickLabel.font = [UIFont systemFontOfSize:12];
  60. self.clickLabel.textColor = [UIColor blueColor];
  61. NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
  62. NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"到这里去" attributes:attribtDic];
  63. self.clickLabel.attributedText = attribtStr;
  64. [self addSubview:self.clickLabel];
  65. [self updatePosition];
  66. }
  67. - (void)setTitle:(NSString *)title
  68. {
  69. self.titleLabel.text = title;
  70. [self updatePosition];
  71. }
  72. - (void)setSubtitle:(NSString *)subtitle
  73. {
  74. self.subtitleLabel.text = [NSString stringWithFormat:@"地址:%@",subtitle];
  75. [self updatePosition];
  76. }
  77. - (void)setPhoneNumber:(NSString *)phoneNumer
  78. {
  79. self.phoneLabel.text = [NSString stringWithFormat:@"电话:%@",phoneNumer];
  80. [self updatePosition];
  81. }
  82. - (void)updatePosition{
  83. NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kTitleSize]};
  84. CGSize titleSize = [self.titleLabel.text boundingRectWithSize:CGSizeMake(200, 5000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
  85. self.titleLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.origin.y, self.titleLabel.frame.size.width, titleSize.height);
  86. CGSize subTitleSize = [self.subtitleLabel.text boundingRectWithSize:CGSizeMake(200, 5000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
  87. self.subtitleLabel.frame = CGRectMake(self.subtitleLabel.frame.origin.x, self.titleLabel.frame.origin.y+self.titleLabel.frame.size.height+kPortraitMargin,self.subtitleLabel.frame.size.width, subTitleSize.height);
  88. self.phoneLabel.frame = CGRectMake(self.phoneLabel.frame.origin.x, self.subtitleLabel.frame.origin.y+self.subtitleLabel.frame.size.height+kPortraitMargin, self.phoneLabel.frame.size.width, self.phoneLabel.frame.size.height);
  89. self.clickLabel.frame = CGRectMake(self.clickLabel.frame.origin.x, self.phoneLabel.frame.origin.y+self.phoneLabel.frame.size.height+kPortraitMargin, self.clickLabel.frame.size.width, self.clickLabel.frame.size.height);
  90. }
  91. - (void)drawRect:(CGRect)rect{
  92. [self drawInContext:UIGraphicsGetCurrentContext()];
  93. self.layer.shadowColor = [[UIColor whiteColor] CGColor];
  94. self.layer.shadowOpacity = 1.0;
  95. self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
  96. }
  97. - (void)drawInContext:(CGContextRef)context
  98. {
  99. CGContextSetLineWidth(context, 2.0);
  100. CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8].CGColor);
  101. [self getDrawPath:context];
  102. CGContextFillPath(context);
  103. }
  104. - (void)getDrawPath:(CGContextRef)context
  105. {
  106. CGRect rrect = self.bounds;
  107. CGFloat radius = 6.0;
  108. CGFloat minx = CGRectGetMinX(rrect),
  109. midx = CGRectGetMidX(rrect),
  110. maxx = CGRectGetMaxX(rrect);
  111. CGFloat miny = CGRectGetMinY(rrect),
  112. maxy = CGRectGetMaxY(rrect)-kArrorHeight;
  113. CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
  114. CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
  115. CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
  116. CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
  117. CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
  118. CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
  119. CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
  120. CGContextClosePath(context);
  121. }
  122. @end
  123. #define kCalloutWidth 220.0
  124. #define kCalloutHeight 150.0
  125. @implementation CustomAnnotationView
  126. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  127. {
  128. if (self.selected == selected)
  129. {
  130. return;
  131. }
  132. if (selected)
  133. {
  134. if (self.calloutView == nil)
  135. {
  136. self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
  137. self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
  138. -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
  139. }
  140. self.calloutView.title = self.annotation.title;
  141. self.calloutView.subtitle = self.annotation.subtitle;
  142. if ([self.annotation isKindOfClass:[CustomAnnotation class]])
  143. {
  144. CustomAnnotation* myAnnotation= self.annotation;
  145. self.calloutView.phoneNumber =myAnnotation.phoneNumber;
  146. self.calloutView.phoneLabel.text =[NSString stringWithFormat:@"电话:%@",myAnnotation.phoneNumber];
  147. self.calloutView.clickLabel.userInteractionEnabled=YES;
  148. UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
  149. [self.calloutView.clickLabel addGestureRecognizer:labelTapGestureRecognizer];
  150. // [self.calloutView.addGestureRecognizer:labelTapGestureRecognizer];
  151. }
  152. [self addSubview:self.calloutView];
  153. }
  154. else
  155. {
  156. [self.calloutView removeFromSuperview];
  157. }
  158. [super setSelected:selected animated:animated];
  159. }
  160. -(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
  161. // UILabel *label=(UILabel*)recognizer.view;
  162. // NSLog(@"%@被点击了",label.text);
  163. if ([self.annotation isKindOfClass:[CustomAnnotation class]])
  164. {
  165. CustomAnnotation* myAnnotation= self.annotation;
  166. [self.channel invokeMethod:@"guide" arguments:myAnnotation.map];
  167. }
  168. }
  169. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  170. UIView *view = [super hitTest:point withEvent:event];
  171. if (view == nil) {
  172. CGPoint tempoint = [self.calloutView.clickLabel convertPoint:point fromView:self];
  173. if (CGRectContainsPoint(self.calloutView.clickLabel.bounds, tempoint)) {
  174. view = self.calloutView.clickLabel;
  175. }
  176. }
  177. return view;
  178. }
  179. @end