Ios 根据swift中的内容,从xib文件自动调整infowindow的大小
我尝试从UIView调整在xib文件中创建的自定义信息窗口的大小。宽度取决于窗口的内容,但我没有找到任何解决方案。我尝试了故事板中的所有操作,但什么都没有发生。我上传了一个屏幕截图以查看我的问题: 以下是我在xib的UIView中的代码Ios 根据swift中的内容,从xib文件自动调整infowindow的大小,ios,swift,xib,autoresize,Ios,Swift,Xib,Autoresize,我尝试从UIView调整在xib文件中创建的自定义信息窗口的大小。宽度取决于窗口的内容,但我没有找到任何解决方案。我尝试了故事板中的所有操作,但什么都没有发生。我上传了一个屏幕截图以查看我的问题: 以下是我在xib的UIView中的代码 public protocol nonPaidDelegate: class { func didTapMapsSelectButton() } class MapInfoWindow: UIView { @IBOutlet weak var titleI
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中,我调用MapInfo窗口
var infoWindow = MapInfoWindow()
infoWindow = loadNiB()
我为标记创建了信息窗口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()
}
}
您可以像这样通过编程实现您的目标,我使用了两个标签(
titleLabel
&snippetLabel
):
并添加此扩展名
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
}
甚至不清楚问题是什么。什么是应该发生的,什么是正在发生的?你的代码应该在哪里实现?我们不知道你在做什么,你想做什么,结果是什么。你们使用了多少标签?你们是对的,我用一些代码更新了我的问题。我认为所有这些代码都可以从xib文件的故事板中完成。我集成了代码,正如我看到的,infoWindow以100%的屏幕开始,它可以根据您在这里输入的int进行更改:infoWindow.frame.size.width=self.view.frame.size.width-50
self.view.frame.size.width-50
是最大宽度如果您的文本大小小于此值,它将很小
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
}