Well, If I got it right, probably what are you looking for is Label intrinsicContentSize:
The natural size for the receiving view, considering only properties
of the view itself.
the width of the label intrinsicContentSize
should be the actual width of the label, doesn't matter what's the frame.size.width
value.
Based on that, you simply implement:
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 21))
print(lbl.frame.size.width)
// 150.0
lbl.text = ""
let intrinsicSizeWidth = lbl.intrinsicContentSize.width
// since the label text is still empty, its value should be 0.0
print(intrinsicSizeWidth)
while lbl.intrinsicContentSize.width < lbl.frame.size.width {
lbl.text?.append(".")
}
print(lbl.text!)
// ................................
Note that increasing your label width would leads to contains more dots:
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
.
.
.
print(lbl.text!)
// ...........................................
Obviously, if you would like to compare it with a UITextField
-for instance- (as you mentioned in the question), it should be:
// "textField" the name of your text field...
while lbl.intrinsicContentSize.width < textField.frame.size.width {
lbl.text?.append(".")
}