我有很长的MKannotationView标题文字。是否有一种简单的方法可以使标题文本适当大小?
reuseId = "Pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.image = UIImage(named:"pin")
pinView?.canShowCallout = true
pinView?.annotation
let button : UIButton = UIButton(type: UIButtonType.DetailDisclosure)
button.setImage(UIImage(named: "pin_arrow")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), forState:UIControlState.Normal)
pinView!.rightCalloutAccessoryView = button
let vw: UIView = UIView(frame: CGRectMake(0, 0, 40, 50))
let label: UILabel = UILabel(frame: CGRectMake(10, 0, 40, 50))
label.font = UIFont.init(name: "Roboto-Bold", size: 20)
label.textColor = UIColor(red:17/255.0, green:97/255.0, blue:172/255.0, alpha: 1.0)
label.numberOfLines = 1
for myObj in arrOfPinsOnMap where myObj.coordinate.latitude == pinView!.annotation!.coordinate.latitude && myObj.coordinate.longitude == pinView!.annotation!.coordinate.longitude && myObj.title == (pinView?.annotation?.title)!{
label.text = myObj.sale
if(myObj.sale != ""){
vw.addSubview(label)
if(label.text!.characters.count == 2){
vw.frame.size.width = 35
label.frame.size.width = 35
} else if(label.text!.characters.count == 3){
vw.frame.size.width = 47
label.frame.size.width = 47
} else if(label.text!.characters.count == 4){
label.frame.size.width = 67
vw.frame.size.width = 67
} else if(label.text!.characters.count > 4){
label.frame.size.width = 80
vw.frame.size.width = 80
}
pinView!.leftCalloutAccessoryView = vw
}
}
我有这种方法在针脚上显示TAP后的View。我可以在左侧调整我的标签尺寸,但标题保持固定大小。标题必须始终可见。
答案 0 :(得分:3)
如您所知,标准MapKit标注具有标题和左右附件视图,看起来您已经发现标注的标题在截断之前可以显示的文本数量有限,我们似乎没有访问标签视图来改变它。
如果您能够定位iOS 9及更高版本,则标题下方会显示额外的detailCalloutAccessoryView
。不幸的是,您不能将标题留空并将所有文本放在详细视图中,因为在点击时标注根本不会打开,因此您需要在标题中添加某种文本。
如果你无法使用它或者你需要支持iOS 8,那么你需要做一个自定义标注。这并不困难。创建一个MKAnnotationView
子类,其具有标注所需的外观,然后当您在mapView(_:didSelectAnnotationView:)
中检测到正在点击的标记时,在与标记相同的位置添加特殊标注注释。
这是一种方法:
class MyAnnotation: NSObject, MKAnnotation
{
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 49.5, longitude: -123.0)
var title: String? = " "
}
class CalloutAnnotation: MyAnnotation { } // Special annotation class for the callout
let calloutRect = CGRect(origin: CGPointZero, size: CGSize(width: 200, height: 100))
class CalloutAnnotationView: MKAnnotationView
{
var label: UILabel
init()
{
label = UILabel(frame: CGRectInset(calloutRect, 8.0, 8.0))
label.textAlignment = .Center
label.numberOfLines = 0
super.init(frame: calloutRect)
self.addSubview(label)
self.backgroundColor = UIColor.whiteColor()
self.centerOffset = CGPoint(x: 0.0, y: -(calloutRect.size.height / 2))
self.layer.cornerRadius = 6.0
}
required init?(coder aDecoder: NSCoder)
{
label = UILabel(frame: CGRectInset(calloutRect, 8.0, 8.0))
super.init(coder: aDecoder)
}
}
class ViewController: UIViewController, MKMapViewDelegate
{
let calloutAnnotation = CalloutAnnotation()
@IBOutlet weak var mapView: MKMapView!
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
mapView.addAnnotation(MyAnnotation())
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
if annotation is CalloutAnnotation
{
let calloutView = CalloutAnnotationView()
calloutView.label.text = "This is a custom callout that can look however you want."
return calloutView
}
else
{
let reuseIdentifier = "pinView"
let pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) ??
MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
pinView.image = UIImage(named: "Pin")
pinView.canShowCallout = false
return pinView
}
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView)
{
if let coordinate = view.annotation?.coordinate
{
calloutAnnotation.coordinate = coordinate
mapView.addAnnotation(calloutAnnotation)
}
}
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView)
{
mapView.removeAnnotation(calloutAnnotation)
}
}
视图只是一个常规视图,可以响应触摸等。这仍然不完美,因为当标注出现时你丢失了'pop'动画,但如果你需要,你可以添加更多的工作至。我没有尝试过,但我确实在某个时刻获得了动画效果,所以我很确定它是可能的。