Reputation: 291
Hi I have a UILabel being added from my code. I have set it as
let lb3 = UILabel(frame: CGRect(x: 0, y: 0, width: summaryView.frame.width - 20, height: 120))
lb3.text = "This is body of the message. This is body of the message. This is body of the message. This is body of the message."
lb3.textAlignment = .left
lb3.numberOfLines = 10
lb3.lineBreakMode = .byWordWrapping
lb3.backgroundColor = UIColor.cyan
lb3.font = UIFont.systemFont(ofSize: 12, weight: .light)
Total height of lb3
is 120 as per snippet
so upper left corner is at coordinate (0, 0)
and bottom right coordinate at (x=width, 120)
lb3
is starting from (0, 60)
assuming this view starts from (0, 0)
but I want lb3
to start from (0, 0)
itself not from its vertical center.
I added lb3 in following manner as this is my view hierarchy
summaryView.addSubview(lb3)
v.addSubview(summaryView)
self.view.addSubview(v)
Upvotes: 0
Views: 134
Reputation: 534893
What you're describing is simply not how UILabels work. Their text is always vertically centered within the height of the label. You are setting the label to be much taller than its text requires, so there is space above the text and below the text.
The usual alternative is to make a UILabel that sets its own height just sufficiently to contain its text (easy to do); that way, the text starts at the top left of the label and fills the label perfectly.
Upvotes: 1
Reputation: 1443
As said before, the text inside a UILabel
will always be vertically centered. A good workaround for you issue would be:
var frame = CGRect(x: 0, y: 0, width: summaryView.frame.width - 20, height: 120)
let lb3 = UILabel(frame: frame)
lb3.text = "This is body of the message. This is body of the message. This is body of the message. This is body of the message."
lb3.textAlignment = .left
lb3.numberOfLines = 10
lb3.lineBreakMode = .byWordWrapping
lb3.backgroundColor = UIColor.cyan
lb3.font = UIFont.systemFont(ofSize: 12, weight: .light)
lb3.sizeToFit()
frame.size.height = lb3.frame.size.height
lb3.frame = frame
Upvotes: 0