简体   繁体   English

如何在swift中制作label背景圆?

[英]How to make label background circle in swift?

在此处输入图像描述

I made the label background a circle I succeeded in creating a circle using the code below我将 label 背景做成了一个圆圈 我使用下面的代码成功创建了一个圆圈

lbNumber.layer.masksToBounds = true
lbNumber.layer.cornerRadius = lbNumber1.frame.width/2

But in the simulator's iphone8, the circle looks distorted as in the photo.但在模拟器的 iphone8 中,圆圈看起来像照片中那样扭曲。 Is there any way to fix this?有没有什么办法解决这一问题? Thank you谢谢

(1) Do not use corner radius to round things. (1)不要用圆角半径来圆东西。 Use a mask.使用口罩。 (2) Do not run the rounding code until you know what the frame is really going to be. (2) 在您知道帧的真正含义之前,不要运行舍入代码。 The problem with your code is that you are running your code too soon (probably in viewDidLoad ), when the true frame is not yet known.您的代码的问题是您运行代码太快(可能在viewDidLoad中),而真正的框架尚不知道。

Make sure it's inside确保它在里面

override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()
  lbNumber.layer.cornerRadius = lbNumber1.frame.height/2
}

Also verify that width = height for the label还要验证 label 的width = height

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

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