Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在显示视图之前,根据特定宽度获取UIView的高度_Ios_Swift_Uiview_Autolayout_Nslayoutconstraint - Fatal编程技术网

Ios 在显示视图之前,根据特定宽度获取UIView的高度

Ios 在显示视图之前,根据特定宽度获取UIView的高度,ios,swift,uiview,autolayout,nslayoutconstraint,Ios,Swift,Uiview,Autolayout,Nslayoutconstraint,我在Interface Builder中设计了一个UIView。它基本上由标题图像和下面的一些文本(UILabel)组成。视图以自定义转换的方式显示,不会填充整个屏幕 左右两侧有20像素的边距,顶部有40像素的边距。ui标签中填充了一些来自web的文本。我想做的是找到(或者说预测)特定宽度下整个视图的高度。我该怎么做 在计算投影大小之前,您需要同时拥有图片和标签。 我想您应该使用类似的方法(可能将imageView和标签之间的垂直间隔距离添加到总和中,也可能从宽度中删除横向边距): 目标C: -

我在Interface Builder中设计了一个
UIView
。它基本上由标题图像和下面的一些文本(
UILabel
)组成。视图以自定义转换的方式显示,不会填充整个屏幕


左右两侧有20像素的边距,顶部有40像素的边距。
ui标签
中填充了一些来自web的文本。我想做的是找到(或者说预测)特定宽度下整个视图的高度。我该怎么做

在计算投影大小之前,您需要同时拥有图片和标签。 我想您应该使用类似的方法(可能将imageView和标签之间的垂直间隔距离添加到总和中,也可能从宽度中删除横向边距):

目标C

- (CGFloat)preferredHeightFromWidth:(CGFloat)width text:(NSString *)text font:(UIFont *)font image:(UIImage *)image
{
    // Calculate label height
    CGFloat labelHeight = [text
                           boundingRectWithSize:CGSizeMake(width, 10000)
                           options:NSStringDrawingUsesLineFragmentOrigin
                           attributes:@{NSFontAttributeName:font}
                           context:[[NSStringDrawingContext alloc] init]
                           ].size.height;

    // Calculate image height
    CGFloat ratio = image.size.height/ image.size.width;
    CGFloat imageHeight = (ratio * width);

    // Do the sum
    return labelHeight + imageHeight;
}
Swift

func preferredHeight(width: CGFloat, text: NSString, font: UIFont, image: UIImage) -> CGFloat {
    // Calculate Label Height
    let labelRect = text.boundingRect(
        with: CGSize.init(width: width, height: 10000),
        options: .usesLineFragmentOrigin,
        attributes: [NSFontAttributeName : font],
        context: NSStringDrawingContext())
    let labelHeight = labelRect.height

    // Calculate Image Height
    let ratio = image.size.height / image.size.width
    let imageHeight = ratio / width

    // Calculate Total Height
    let height = labelHeight + imageHeight

    // Return Height Value
    return height
}

(感谢克里斯托弗·汉纳提供的swift版本)

这里的答案与阿尔贝托的答案相同,但我已将其改为swift 3

func preferredHeight(width: CGFloat, text: NSString, font: UIFont, image: UIImage) -> CGFloat {
    // Calculate Label Height
    let labelRect = text.boundingRect(
        with: CGSize.init(width: width, height: 10000),
        options: .usesLineFragmentOrigin,
        attributes: [NSFontAttributeName : font],
        context: NSStringDrawingContext())
    let labelHeight = labelRect.height

    // Calculate Image Height
    let ratio = image.size.height / image.size.width
    let imageHeight = ratio / width

    // Calculate Total Height
    let height = labelHeight + imageHeight

    // Return Height Value
    return height
}

我也遇到过类似的问题,但您可以使用此
UIView
扩展名来自动调整视图的最大宽度,而不是计算字体大小和图像高度:

extension UIView {

    func autosize(maxWidth: CGFloat) {

        translatesAutoresizingMaskIntoConstraints = false

        let dummyContainerView = UIView(frame: CGRect(x: 0, y: 0, width: maxWidth, height: 10000000))
        dummyContainerView.addSubview(self)
        dummyContainerView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        dummyContainerView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
        dummyContainerView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true

        setNeedsLayout()
        layoutIfNeeded()

        removeFromSuperview()

        frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)

        translatesAutoresizingMaskIntoConstraints = true
    }
} 
使用这种方法,您不必担心视图中的内容

要使用它:

let customView: CustomView = ... //create your view
... // configure all data on your view, e.g. labels with correct text
customView.autosize(maxWidth: 150) // resize your view 
view.addSubview(customView) // add your view to any view

您对该视图有什么限制?顶部图像具有固定高度,固定在顶部、左侧和右侧,UILabel也固定在顶部、左侧、右侧和底部。您能显示一些图像吗?你核对过这个答案了吗?