简体   繁体   中英

How do you position a UILabel in Swift by setting the position of it's top left corner?

so I'm working with a UILabel in Swift and am trying to set the position of the label so its top left corner is what I am setting the position of. As it currently stands, whenever I change the position by changing the label's x and y values, it positions based off the center of the label. This is problematic because the contents of the label change sometimes...this is why I'd like to position the whole element by positioning the top left corner (because that won't change depending on the size of the content, unlike the center of the UILabel). If I need to add code to make this more comprehensible, please let me know!

The label's frame has a position based on it's top left corner, that's what you want to manipulate if you're trying to position it based on that corner.

Label with top left position (0,0):

let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))

Move label down and right, the label's top left corner will be at (10,25):

myLabel.frame = CGRect(x: 10, y: 25, width: myLabel.frame.size.width, height: myLabel.frame.size.height)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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