MapView 注释中的较长字幕 (swift)
Longer subtitles in MapView annotations (swift)
我有一个 mapView,带有显示标题和副标题的注释。字幕有时比注释的宽度长,所以我想知道是否可以将它们设为多行?
到目前为止它的编码是这样的:
func annotate(newCoordinate, title: String, subtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
annotation.subtitle = subtitle
self.map.addAnnotation(annotation)
}
然后我在
中设置了几个选项
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}
与此处无关。
是否可以制作自定义注释视图?
我尝试了几件事,但没有任何效果。我能得到的最接近的是添加一个按钮来单独显示较长的字幕,但我宁愿将它放在注释中。
可能吗?
我想通了,我在 viewForAnnotation 中添加了一个标签,它起作用了
¯\_(ツ)_/¯
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"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
}
else {
pinView!.annotation = annotation
}
//THIS IS THE GOOD BIT
let subtitleView = UILabel()
subtitleView.font = subtitleView.font.fontWithSize(12)
subtitleView.numberOfLines = 0
subtitleView.text = annotation.subtitle!
pinView!.detailCalloutAccessoryView = subtitleView
return pinView
}
我有一个 mapView,带有显示标题和副标题的注释。字幕有时比注释的宽度长,所以我想知道是否可以将它们设为多行? 到目前为止它的编码是这样的:
func annotate(newCoordinate, title: String, subtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
annotation.subtitle = subtitle
self.map.addAnnotation(annotation)
}
然后我在
中设置了几个选项func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}
与此处无关。
是否可以制作自定义注释视图? 我尝试了几件事,但没有任何效果。我能得到的最接近的是添加一个按钮来单独显示较长的字幕,但我宁愿将它放在注释中。
可能吗?
我想通了,我在 viewForAnnotation 中添加了一个标签,它起作用了
¯\_(ツ)_/¯
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"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
}
else {
pinView!.annotation = annotation
}
//THIS IS THE GOOD BIT
let subtitleView = UILabel()
subtitleView.font = subtitleView.font.fontWithSize(12)
subtitleView.numberOfLines = 0
subtitleView.text = annotation.subtitle!
pinView!.detailCalloutAccessoryView = subtitleView
return pinView
}