简体   繁体   English

如何在iOS / Swift中为指定宽度创建固定宽度标签?

[英]How to create fixed width label for specified width in iOS/Swift?

Say I'm creating a calculator and I know I'm going to support a maximum of 20 digits in portrait and 40 digits in landscape. 假设我正在创建一个计算器,并且我知道我将最多支持纵向20位和横向40位。

How do I create or resize a label to display up to my maximum number of digits such that it doesn't keep resizing as we add or remove digits from the result? 如何创建或调整标签大小以显示最多我的最大位数,以便在我们从结果中添加或删除位数时,它不会保持调整大小?

Code I've tried so far; 到目前为止我尝试过的代码;

let labelFont : UIFont = resultDigitsLabel.font;
let originalString : String = "00000000000000000000";
let nsString : NSString = originalString as NSString;
var labelSize : CGSize = nsString.size(attributes: [NSFontAttributeName: labelFont]);
labelSize.height = 0;
let labelRect : CGRect = resultDigitsLabel.frame;
let newFrame :CGRect = CGRect(origin: labelRect.origin, size: labelSize);
resultDigitsLabel.frame = newFrame;

resultDigitsLabel.layer.borderWidth = 0.5;
resultDigitsLabel.layer.cornerRadius = 5.0;

So far no luck, the original frame reset doesn't appear to resize the label and as I add or remove digits the frame (by watching the border) resizes also. 到目前为止,没有运气,原始的帧重置似乎不会调整标签的大小,并且当我添加或删除数字时(通过观察边框),帧的大小也会调整。

I don't have any size constraints that I see, should I be creating a constraint rather than setting the width by altering the frame? 我没有看到任何尺寸限制,应该创建一个限制而不是通过更改框架来设置宽度吗?

Any other tips? 还有其他提示吗?

Thanks. 谢谢。

尝试将此属性添加到标签:-resultDigitsLabel.adjustsFontSizeToFitWidth = true

You have to calculate the frame for 20 digits or 40 digits and create the frame for UILabel with that frame. 您必须计算20位数或40位数的框架,并使用该框架为UILabel创建框架。

let digits:NSString = "99999999999999999999" as NSString
let idealFrame= digits.boundingRect(with: CGSize(width: CGFloat.infinity, height: 30), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName:labelFont], context: nil).size

Now you can make UILabel this frame. 现在,您可以使UILabel成为此框架。

Constraint is the best way. 约束是最好的方法。 Give Leading and Trailing constraint to your Label and also give equal width with the super view. 将“ Leading和“ Trailing约束赋予您的“标签”,并提供与超级视图equal width It will support in both Landscape and Portrait mode. 它将支持横向和纵向模式。

As per thewarri0r9 above adding constraints did the trick; 按照上面的thewarri0r9的要求,增加了限制。

    let screenSize: CGRect = UIScreen.main.bounds;
    let screenWidth = screenSize.width;
    let constraintMargin = (screenWidth - labelSize.width) / 2;
    let leadingConstraint = NSLayoutConstraint(item: targetDigitsLabel, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: constraintMargin);
    let trailingConstraint = NSLayoutConstraint(item: targetDigitsLabel, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: constraintMargin * -1);
    view.addConstraints([leadingConstraint, trailingConstraint]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM