Swift - Custom Mkannotationview, Set Label Title

Adding a title in front/below a custom annotation mapkit swift 4

You can create a label like so:

let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
annotationLabel.numberOfLines = 3
annotationLabel.textAlignment = .center
annotationLabel.font = UIFont(name: "Rockwell", size: 10)
// you can customize it anyway you want like any other label

Set the text:

annotationLabel.text = annotation.title!!

And then add to annotation view:

annotationView.addSubview(annotationLabel)

Picture of annotation with label

I also added a background and border by doing:

annotationLabel.backgroundColor = UIColor.white
annotationLabel.layer.cornerRadius = 15
annotationLabel.clipsToBounds = true

You can also change where the label is in respect to the annotation by changing the X and Y when creating the label. Negative is to the left and up, positive right and down.

How we can put label on custom pin in MapKit?

If you mean you want the pin to have a letter on it then you need to set the image of the annotation view in viewForAnnotation. If you're planning to change the pin for each annotation you'll need to create the image dynamically. They will be plenty of code around this but it comes down to this

annotationView.image = anImage;

Show Annotation Title and SubTitle in Custom MKPinAnnotationView

I was overriding one func

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
mapView.deselectAnnotation(view.annotation, animated: true)
}

I commented this function out and got the titles and subtitles.

Updated Code

/*
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
mapView.deselectAnnotation(view.annotation, animated: true)
}
*/
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}

let reuseId = "pin"
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView.canShowCallout = true
pinView.animatesDrop = true
pinView.pinTintColor = UIColor.darkGrayColor()
pinView.draggable = true
pinView.accessibilityLabel = "hello"
let btn = UIButton(type: .DetailDisclosure)
pinView.rightCalloutAccessoryView = btn
return pinView
}

Why doesn't my labels text change when reusing annotation views mapkit swift 4

When the annotation is not nil and control goes to else the title of the label is changed but it's not the label that was added to the annotationView , it's the local varaible that will be deallocated when func returns

annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)

so you have to query the view for that label may be by a tag that you give it before adding and assign the text to it

//

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }

var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
annotationLabel.numberOfLines = 3
annotationLabel.textAlignment = .center
annotationLabel.font = UIFont(name: "Rockwell", size: 10)
annotationLabel.tag = 22
let strokeTextAttributes: [NSAttributedStringKey: Any] = [
.strokeColor : UIColor.white,
.foregroundColor : UIColor.black,
.strokeWidth : -4,
]

if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView?.animatesDrop = true
annotationView?.canShowCallout = false

annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
annotationView?.addSubview(annotationLabel)
} else {
annotationView?.annotation = annotation

for item in annotationView!.subviews {
if item.tag == 22 {
let lbl = item as! UILabel
lbl.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
break
}
}

}

annotationLabel.clipsToBounds = true
annotationLabel.backgroundColor = UIColor.white
annotationLabel.layer.cornerRadius = 15

return annotationView
}


Related Topics



Leave a reply



Submit