如何使Swift中的UILabel变成一个圆圈 [英] How to make UILabel in Swift a circle

查看:339
本文介绍了如何使Swift中的UILabel变成一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Swift中的UILabel成为一个完美的圈子.我当前正在使用以下内容:

I am trying to make a UILabel in Swift a perfect circle. I am currently using the following:

pResult.layer.masksToBounds = true
pResult.layer.cornerRadius = 125

这个问题是,它可以在6s加上+的情况下正常工作,但任何其他尺寸都不会变成圆形.最好的方法是什么?

The problem with this is that it works fine on 6s plus but any other size it does not become a circle. What is the best way to do this?

推荐答案

圆角还是整圆?无论如何,假设您想要第二个选项,则应在情节提要上将纵横比(width:height)限制为1:1,因此标签始终是正方形.然后,在代码中,您可以执行

Circled corners or a full circle? Anyway, assuming that you want the second option, you should constraint the aspect ratio (width:height) to 1:1 on the storyboard so the label is always a square. Then, in the code, you can just do something like

pResult.layer.cornerRadius = pResult.frame.width/2

无论屏幕大小如何,始终使其成为一个完美的圆圈.

to always make it a perfect circle, no matter what screen size it will be on.

这篇关于如何使Swift中的UILabel变成一个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆