MapView批注中的较长字幕(快速) [英] Longer subtitles in MapView annotations (swift)
问题描述
我有一个mapView带有显示标题和字幕的注释.字幕有时长于注释的宽度,所以我想知道我是否可以使它们多行? 到目前为止,它是这样编码的:
I have a mapView with annotations displaying titles and subtitles. The subtitles are sometimes longer than the width of the annotation, so I am wondering if i can make them multiline? It's coded like this so far:
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? {...}
与此处无关.
可以创建自定义注释视图吗? 我已经尝试了几件事,但是没有任何效果.我能得到的最接近的效果是添加一个按钮以分别显示较长的字幕,但我宁愿将其放在批注中.
Is it posible to make a custom annotation view? I've tried a couple of things, but nothing worked. The closest I can get is adding a button to display the longer subtitle separately, but i'd rather have it inside the annotation.
有可能吗?
推荐答案
我弄清楚了,我在viewForAnnotation中添加了一个标签,它就可以了
I figured it out, I added a label in viewForAnnotation and it just worked
¯\ _(ツ)_/¯
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批注中的较长字幕(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!