2016-05-24 14 views
2

我有很長的MKannotationView標題文本。有沒有一種簡單的方法可以讓標題的文字大小合適?Swift:MKAnnotation長標題文本

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之後的視圖。我可以在左側調整我的標籤大小,但標題保持固定大小。標題必須始終可見。

回答

1

如您所知,標準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) 
    } 
} 

的觀點僅僅是一個普通視圖,因爲你失去的「流行」動畫標註出現時可以以觸摸等,這還不夠完善響應,但如果需要的話,您可以添加更多的工作。我還沒有嘗試過,但是我確實在某一時刻在動畫中淡入淡出,所以我相當確信這是可能的。

+0

我的問題是不同的,但'var title:String? =「」'引發瞭如何解決我的問題的想法。對那個朋友+1。 – iUser