Reputation: 101
I've confirmed, that the value given to "addLabel" function, is changing, but when printing out "label.frame.origin.y", it's always showing 0. At the moment, the code looks pretty bad, because I've been struggling with this issue for a while now. Any ideas?
var newY: Int = 8
var mainViewNewHeight: Int = 251
func setContentFromList(content: [String], target: UIView, targetHeight: NSLayoutConstraint) {
newY = 8
var bullet:UILabel!
let width = Int(UIScreen.main.bounds.width) - 84
let font = UIFont(name: "Helvetica", size: 18)
for item in content {
bullet = UILabel(frame: CGRect(x: 8, y: newY, width: 7, height: 17))
bullet.text = "•"
bullet.textColor = UIColor( red: 183/255, green: 110/255, blue:121/255, alpha: 1.0 )
bullet.font = bullet.font.withSize(18)
let a = CGFloat(newY)
print(a) // Correct
addLabel(y: a, text: item, font: font!, width: CGFloat(width), target: target)
target.addSubview(bullet)
}
mainViewNewHeight += newY
targetHeight.constant = CGFloat(newY)
}
And this is the "addLabel" function:
func addLabel(y: CGFloat, text:String, font:UIFont, width:CGFloat, target: UIView) {
let label:UILabel = UILabel(frame: CGRect(x: 22, y: y, width: width, height: CGFloat.greatestFiniteMagnitude))
print("Label y:", label.frame.origin.y) // Invalid (0.0)
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
newY += Int(label.frame.height)
target.addSubview(label)
}
Upvotes: 0
Views: 137
Reputation: 38833
Your issue is in the height: CGFloat.greatestFiniteMagnitude
remove CGFloat.greatestFiniteMagnitude
and replace it with an integer value
and you´ll see that your code will work.
So basically:
let label:UILabel = UILabel(frame: CGRect(x: 22, y: y, width: width, height: 20))
Print out:
20.0
Label y: 20.0
41.0
Label y: 41.0
62.0
Label y: 62.0
83.0
Label y: 83.0
Upvotes: 1