how to show multiple lines in MKAnnotation with autolayout?

后端 未结 6 1903
臣服心动
臣服心动 2020-12-30 17:21

i am using mapkit how i can multiple line in MKAnnotation view.

Every annotation there has title and subtitle. how i show sub title with

6条回答
  •  我在风中等你
    2020-12-30 18:07

    we can show multiple line in MKAnnotation view With the help of auto layout

    it's very simple.

    in objective c

     - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    
            if ([annotation isKindOfClass:[MKUserLocation class]])
                return nil;
            if ([annotation isKindOfClass:[CustomAnnotation class]]) {
                CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
    
                MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];
    
                if (annotationView == nil)
                    annotationView = customAnnotation.annotationView;
                else
                    annotationView.annotation = annotation;
    
                //Adding multiline subtitle code 
    
                UILabel *subTitlelbl = [[UILabel alloc]init];
                subTitlelbl.text = @"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon.  ";
    
                annotationView.detailCalloutAccessoryView = subTitlelbl;
    
                NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150];
    
                 NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
                [subTitlelbl setNumberOfLines:0];
                [subTitlelbl addConstraint:width];
                [subTitlelbl addConstraint:height];
    
    
    
    
                return annotationView;
            } else
                return nil;
        }
    

    output

    For Swift

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    
            let identifier = "MyPin"
    
            if annotation.isKindOfClass(MKUserLocation) {
                return nil
            }
    
            var annotationView: MKPinAnnotationView? = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? MKPinAnnotationView
    
            if annotationView == nil {
    
                annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                annotationView?.canShowCallout = true
    
                  let label1 = UILabel(frame: CGRectMake(0, 0, 200, 21))
                label1.text = "Some text1 some text2 some text2 some text2 some text2 some text2 some text2"
                 label1.numberOfLines = 0
                  annotationView!.detailCalloutAccessoryView = label1;
    
                let width = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
                label1.addConstraint(width)
    
    
                let height = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90)
                label1.addConstraint(height)
    
    
    
            } else {
                annotationView!.annotation = annotation
            }
            return annotationView
        }
    
    
    }
    

    here i use NSLayoutConstraint

    i programatically create a label. add the constraint on it and then add the label in detailCalloutAccessoryView of MKAnnotationView.

提交回复
热议问题