从xib文件中自动调整infowindow取决于swift中的内容

时间:2018-04-05 11:22:34

标签: ios swift xib autoresize

我尝试调整我在UIView的xib文件中创建的自定义信息窗口。宽度取决于窗口的内容,但我找不到任何解决方案。我尝试了故事板中的所有内容但没有任何反应。我有上传截图以查看我的问题:

这是来自xib的UIView的代码

public protocol nonPaidDelegate:  class {
func didTapMapsSelectButton()
}

class MapInfoWindow: UIView {

@IBOutlet weak var titleInfo: UILabel!
@IBOutlet weak var adressInfo: UILabel!
@IBOutlet weak var buttonAction: UIButton!

open var delegate:nonPaidDelegate?
@IBAction func didTapInButton(_ sender: Any) {

    self.delegate?.didTapMapsSelectButton()

    print("button tapped")
}


class func instanceFromNib() -> UIView {
    return UINib(nibName: "MapInfoWindowView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
}

在我的viewcontroller中,我调用了MapInfoWindow

var infoWindow = MapInfoWindow()
 infoWindow = loadNiB()

我为标记创建了infoWindow UIView:

    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {

    if let venueItem = marker.userData as? Venue {
        infoWindow.titleInfo?.text = venueItem.name
        infoWindow.adressInfo?.text = venueItem.locationName
        NSLog(venueItem.name!)
    } else {
        NSLog("Did tap a normal marker")
    }

    return UIView()
}

}

screenshot from the view

1 个答案:

答案 0 :(得分:0)

您可以通过编程方式实现目标,我使用了两个标签(titleLabel& snippetLabel):

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
            let infoWindowView = UIView()
            let titleStringWidth = marker.title?.widthOfString(usingFont: UIFont.boldSystemFont(ofSize: 13.0))
            var infoWidth = 0
                if titleStringWidth!+10>self.view.frame.size.width - 50 {
                  infoWindowView.frame.size.width = self.view.frame.size.width - 50
                  infoWidth = Int(self.view.frame.size.width - 50.0)
                }else {
                    infoWindowView.frame.size.width = titleStringWidth!+10
                    infoWidth = Int(titleStringWidth!)+10
                }

            infoWindowView.frame.size.width = self.view.frame.size.width - 50
            infoWindowView.backgroundColor  = UIColor.white
            let titleLabel = UILabel()
            titleLabel.frame.size.width  = infoWindowView.frame.size.width-10
            titleLabel.numberOfLines     = 0
            titleLabel.lineBreakMode     = .byWordWrapping
            titleLabel.font = UIFont.boldSystemFont(ofSize: 13.0)
            titleLabel.frame.origin.x    = 3
            titleLabel.frame.origin.y    = 5

            titleLabel.text = "your text"
            titleLabel.textAlignment     = .center
            titleLabel.sizeToFit()
            let actualNumberOfLinesForTiltle = titleLabel.numberOfVisibleLines
            titleLabel.frame.size.height = CGFloat(actualNumberOfLinesForTiltle*20)

            let snippetLabel = UILabel()
            snippetLabel.frame.size.width = infoWindowView.frame.size.width-10
            snippetLabel.numberOfLines = 0
            snippetLabel.lineBreakMode = .byWordWrapping
            snippetLabel.font = UIFont.boldSystemFont(ofSize: 13.0)
            snippetLabel.text = "your text"
            snippetLabel.textAlignment = .center
            snippetLabel.sizeToFit()
            let snippet_y = 20*actualNumberOfLinesForTiltle+10
            let actualNumberOfLinesForSnippet = snippetLabel.numberOfVisibleLines
            snippetLabel.frame.origin.x = 3
            snippetLabel.frame.origin.y = CGFloat(snippet_y)
            snippetLabel.frame.size.height = CGFloat(actualNumberOfLinesForSnippet*20)

            let visibleHeightOfView = 15+actualNumberOfLinesForTiltle*20+actualNumberOfLinesForSnippet*20
            infoWindowView.frame = CGRect(x: 0, y: 0, width: infoWidth, height: visibleHeightOfView)

            infoWindowView.addSubview(titleLabel)
            infoWindowView.addSubview(snippetLabel)
        return infoWindowView    
}            

并添加此扩展程序

extension UILabel {
    var numberOfVisibleLines: Int {
        let textSize = CGSize(width: CGFloat(self.frame.size.width), height: CGFloat(MAXFLOAT))
        let rHeight: Int = lroundf(Float(self.sizeThatFits(textSize).height))
        let charSize: Int = lroundf(Float(self.font.pointSize))
        return rHeight / charSize
    }

}
extension String {

    func widthOfString(usingFont font: UIFont) -> CGFloat {
        let fontAttributes = [NSFontAttributeName: font]
        let size = self.size(attributes: fontAttributes)
        return size.width
    }