// // CustomCalloutView.m // amap_location // // Created by i2edu on 2020/4/23. // #import "CustomCalloutView.h" #import "CustomAnnotation.h" #define kArrorHeight 10 #define kPortraitMargin 5 #define kTitleWidth 190 #define kTitleHeight 20 #define kTitleSize 12 #define kSubTitleSize 12 #define kPhoneNumSize 12 #define kGotoSize 12 @interface CustomCalloutView () @property (nonatomic, strong) UILabel *subtitleLabel; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *phoneLabel; @property (nonatomic, strong) UILabel *clickLabel; @end @implementation CustomCalloutView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self initSubViews]; } return self; } - (void)initSubViews { // 添加标题,即商户名 self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 , kPortraitMargin, kTitleWidth, kTitleHeight)]; self.titleLabel.font = [UIFont boldSystemFontOfSize:kTitleSize]; self.titleLabel.textColor = [UIColor blackColor]; self.titleLabel.text = _title; self.titleLabel.numberOfLines=0; [self addSubview:self.titleLabel]; // 添加副标题,即商户地址 self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)]; self.subtitleLabel.font = [UIFont systemFontOfSize:kSubTitleSize]; self.subtitleLabel.textColor = [UIColor grayColor]; self.subtitleLabel.text = [NSString stringWithFormat:@"地址:%@",_subtitle]; self.subtitleLabel.numberOfLines=0; [self addSubview:self.subtitleLabel]; // 店家d电话 self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2, kPortraitMargin * 2 + kTitleHeight*2, kTitleWidth, kTitleHeight)]; self.phoneLabel.font = [UIFont systemFontOfSize:kSubTitleSize]; self.phoneLabel.textColor = [UIColor grayColor]; self.phoneLabel.text = [NSString stringWithFormat:@"电话:%@",_phoneNumber]; self.phoneLabel.numberOfLines=0; [self addSubview:self.phoneLabel]; // 添加到这里去 self.clickLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 , kPortraitMargin * 2 + kTitleHeight*3, kTitleWidth, kTitleHeight)]; self.clickLabel.font = [UIFont systemFontOfSize:12]; self.clickLabel.textColor = [UIColor blueColor]; NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}; NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"到这里去" attributes:attribtDic]; self.clickLabel.attributedText = attribtStr; [self addSubview:self.clickLabel]; [self updatePosition]; } - (void)setTitle:(NSString *)title { self.titleLabel.text = title; [self updatePosition]; } - (void)setSubtitle:(NSString *)subtitle { self.subtitleLabel.text = [NSString stringWithFormat:@"地址:%@",subtitle]; [self updatePosition]; } - (void)setPhoneNumber:(NSString *)phoneNumer { self.phoneLabel.text = [NSString stringWithFormat:@"电话:%@",phoneNumer]; [self updatePosition]; } - (void)updatePosition{ NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kTitleSize]}; CGSize titleSize = [self.titleLabel.text boundingRectWithSize:CGSizeMake(200, 5000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; self.titleLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.origin.y, self.titleLabel.frame.size.width, titleSize.height); CGSize subTitleSize = [self.subtitleLabel.text boundingRectWithSize:CGSizeMake(200, 5000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; 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); 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); 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); } - (void)drawRect:(CGRect)rect{ [self drawInContext:UIGraphicsGetCurrentContext()]; self.layer.shadowColor = [[UIColor whiteColor] CGColor]; self.layer.shadowOpacity = 1.0; self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); } - (void)drawInContext:(CGContextRef)context { CGContextSetLineWidth(context, 2.0); CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8].CGColor); [self getDrawPath:context]; CGContextFillPath(context); } - (void)getDrawPath:(CGContextRef)context { CGRect rrect = self.bounds; CGFloat radius = 6.0; CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect); CGFloat miny = CGRectGetMinY(rrect), maxy = CGRectGetMaxY(rrect)-kArrorHeight; CGContextMoveToPoint(context, midx+kArrorHeight, maxy); CGContextAddLineToPoint(context,midx, maxy+kArrorHeight); CGContextAddLineToPoint(context,midx-kArrorHeight, maxy); CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius); CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius); CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius); CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); CGContextClosePath(context); } @end #define kCalloutWidth 220.0 #define kCalloutHeight 150.0 @implementation CustomAnnotationView - (void)setSelected:(BOOL)selected animated:(BOOL)animated { if (self.selected == selected) { return; } if (selected) { if (self.calloutView == nil) { self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)]; self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x, -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y); } self.calloutView.title = self.annotation.title; self.calloutView.subtitle = self.annotation.subtitle; if ([self.annotation isKindOfClass:[CustomAnnotation class]]) { CustomAnnotation* myAnnotation= self.annotation; self.calloutView.phoneNumber =myAnnotation.phoneNumber; self.calloutView.phoneLabel.text =[NSString stringWithFormat:@"电话:%@",myAnnotation.phoneNumber]; self.calloutView.clickLabel.userInteractionEnabled=YES; UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)]; [self.calloutView.clickLabel addGestureRecognizer:labelTapGestureRecognizer]; // [self.calloutView.addGestureRecognizer:labelTapGestureRecognizer]; } [self addSubview:self.calloutView]; } else { [self.calloutView removeFromSuperview]; } [super setSelected:selected animated:animated]; } -(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{ // UILabel *label=(UILabel*)recognizer.view; // NSLog(@"%@被点击了",label.text); if ([self.annotation isKindOfClass:[CustomAnnotation class]]) { CustomAnnotation* myAnnotation= self.annotation; [self.channel invokeMethod:@"guide" arguments:myAnnotation.map]; } } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *view = [super hitTest:point withEvent:event]; if (view == nil) { CGPoint tempoint = [self.calloutView.clickLabel convertPoint:point fromView:self]; if (CGRectContainsPoint(self.calloutView.clickLabel.bounds, tempoint)) { view = self.calloutView.clickLabel; } } return view; } @end