根据文本值动态更新 UILabel 在 UIView 上的位置
Dynamically update UILabel's position on UIView depend on their text value
我从网络上获取这些值。如果所有值都来自网络,则此屏幕看起来很酷。
在没有数据的情况下,它看起来如下。
我使用了这个代码
//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;
}
那么我该如何更新标签框架,以便任何标签都可以占据空白标签的位置?
为您的标签使用 Storyboard 并且仅使用单个标签
将标签设置为具有所有 4 个约束,左、右、上、下
设置行数 0。
它适用于您的文字
我从网络上获取这些值。如果所有值都来自网络,则此屏幕看起来很酷。
在没有数据的情况下,它看起来如下。
我使用了这个代码
//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;
}
那么我该如何更新标签框架,以便任何标签都可以占据空白标签的位置?
为您的标签使用 Storyboard 并且仅使用单个标签 将标签设置为具有所有 4 个约束,左、右、上、下 设置行数 0。 它适用于您的文字