动态更新UILabel的位置上的UIView取决于他们的文本值 [英] Dynamically update UILabel's position on UIView depend on their text value

查看:99
本文介绍了动态更新UILabel的位置上的UIView取决于他们的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网络上获得这些值。该屏幕如果所有值从网络来到这看起来很酷。

I am getting these values from web. This screen if all values came from web which looks cool.

在这里输入的形象描述

而在没有数据的情况看起来如下:

And in absence of data it looks like follows.

在这里输入的形象描述

我用这个code

//Purchasae info
    {
        NSInteger heightToDecrease = 0;
        NSInteger heightToIncrease = 0;
        // NSInteger LABEL_HEIGHT = 18;

        maximumSize = CGSizeZero;
        textSize = CGSizeZero;

        //purchaseCostLabel
        heightToDecrease += self.PurchsCostHeightConstrains.constant;
        maximumSize = CGSizeMake(self.purchaseCostLabel.frame.size.width, 999);
        textSize = [UtilityHandler textSizeOfString:self.purchaseCostLabel.text font:self.purchaseCostLabel.font maxSize:maximumSize];
        NSInteger purchaseCostLabelHeight = textSize.height+SPACE_PADDING;
        self.PurchsCostHeightConstrains.constant = purchaseCostLabelHeight;
        totalHeightToIncrease += self.PurchsCostHeightConstrains.constant;

        //insurance
        heightToDecrease += self.insuranceHeightConstraints.constant;
        maximumSize = CGSizeMake(self.purchaseInsuranceLabel.frame.size.width, 999);
        textSize = [UtilityHandler textSizeOfString:self.purchaseInsuranceLabel.text font:self.purchaseInsuranceLabel.font maxSize:maximumSize];
        NSInteger insCostLabelHeight = textSize.height+SPACE_PADDING;
        self.insuranceHeightConstraints.constant = insCostLabelHeight;
        totalHeightToIncrease += self.insuranceHeightConstraints.constant;

        //shipping
        heightToDecrease += self.shippingHeightConstarants.constant;
        maximumSize = CGSizeMake(self.shippingLabel.frame.size.width, 999);
        textSize = [UtilityHandler textSizeOfString:self.shippingLabel.text font:self.shippingLabel.font maxSize:maximumSize];
        NSInteger shippingLabelHeight = textSize.height+SPACE_PADDING;
        self.shippingHeightConstarants.constant = shippingLabelHeight;
        totalHeightToIncrease += self.shippingHeightConstarants.constant;

        //conservation
        heightToDecrease += self.conserVHeightConstraints.constant;
        maximumSize = CGSizeMake(self.conservationLabel.frame.size.width, 999);
        textSize = [UtilityHandler textSizeOfString:self.conservationLabel.text font:self.conservationLabel.font maxSize:maximumSize];
        NSInteger conserLabelHeight = textSize.height+SPACE_PADDING;
        self.conserVHeightConstraints.constant = conserLabelHeight;
        totalHeightToIncrease += self.conserVHeightConstraints.constant;
        //storage
        heightToDecrease += self.storageHeightConstraints.constant;
        maximumSize = CGSizeMake(self.storageLabel.frame.size.width, 999);
        textSize = [UtilityHandler textSizeOfString:self.storageLabel.text font:self.storageLabel.font maxSize:maximumSize];
        NSInteger storagLabelHeight = textSize.height+SPACE_PADDING;
        self.storageHeightConstraints.constant = storagLabelHeight;
        totalHeightToIncrease += self.storageHeightConstraints.constant;
        //Frameing
        heightToDecrease += self.framingHeightConstraints.constant;
        maximumSize = CGSizeMake(self.shippingLabel.frame.size.width, 999);
        textSize = [UtilityHandler textSizeOfString:self.shippingLabel.text font:self.shippingLabel.font maxSize:maximumSize];
        NSInteger framLabelHeight = textSize.height+SPACE_PADDING;
        self.framingHeightConstraints.constant = framLabelHeight;
        totalHeightToIncrease += self.framingHeightConstraints.constant;

        // set content view height constraint
        self.purchaseInformationViewHeightConstraint.constant -= heightToDecrease;
        // set content view height constraint
        self.purchaseInformationViewHeightConstraint.constant += heightToIncrease;


    }

那么,如何更新标签帧,所以如果任何标签可以采取空白标签的位置?

So how do I update labels frame so if any label can take blank labels' positions?

推荐答案

使用故事板为您的标签,并且只使用单一的标签
设置标签将所有4约束,左,右,上,下
设置的行数为0。
它会为你的文字工作。

Use Storyboard for your label and use only single label Set your label to have all 4 constraints, left,right,top,bottom Set number of lines 0. It will work for your text

这篇关于动态更新UILabel的位置上的UIView取决于他们的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆