如何在 iPhone 上创建圆角 UILabel?
是否有内置方法来创建圆角 UILabels? 如果答案是否定的,那么如何创建这样一个对象呢?
Is there a built in way to create round-cornered UILabels? If the answer is no, how would one go about creating such an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我创建了一个 swift UILabel 子类来实现此效果。 此外,我自动将文本颜色设置为黑色或白色以获得最大对比度。
结果
使用过的 SO-Posts:
Playground
只需将其粘贴到 iOS Playground 中:
I made a swift
UILabel
subclass to achieve this effect. In addition I automatically set the text color to either black or white for maximal contrast.Result
Used SO-Posts:
Playground
Just paste this into an iOS Playground:
如果你想要 UI 对象的圆角,例如(
UILabel
、UIView
、UIButton
、UIImageView
),请通过 Storyboard 设置clip to borders
true 并将用户定义的运行时属性
键路径设置为layer.cornerRadius
,类型=数字和值= 9(根据您的要求)If you want rounded corner of UI objects like (
UILabel
,UIView
,UIButton
,UIImageView
) by storyboard then setclip to bounds
true and setUser Defined Runtime Attributes
Key path aslayer.cornerRadius
, type = Number and value = 9 (as your requirement)xCode 7.3.1 iOS 9.3.2
xCode 7.3.1 iOS 9.3.2
另一种方法是将 png 放在 UILabel 后面。 我有多个标签的视图,这些标签覆盖一个背景 png,其中包含各个标签的所有图稿。
Another method is to place a png behind the UILabel. I have views with several labels that overlay a single background png that has all the artwork for the individual labels.
.cornerRadius
是设置圆角边缘的关键属性。 如果您对应用程序中的所有标签使用相同的样式,我建议您使用扩展方法。代码:
输出:
为什么使用扩展方法?
创建一个 Swift 文件并添加以下代码,其中包含“UILabel”类的扩展方法,其中此方法是用户定义的,但适用于应用程序中的所有标签,并且如果您将来更改任何样式,只需在扩展方法中更改,将有助于保持一致性和干净的代码。
.cornerRadius
is the key property to set rounded edges. If you are using the same style for all labels in your application, I would recommend for an extension method.Code:
Output:
Why Extension method?
Create a Swift file and add the following code, which has the Extention method to the "UILabel" class, where this method is user defined but will work for all the label in your application and will help to maintain consistency and clean code, if you change any style in future require only in the extension method.
在 Monotouch / Xamarin.iOS 中我解决了同样的问题,如下所示:
In Monotouch / Xamarin.iOS I solved the same problem like this:
在 Swift 2.0 中完美运行
Works perfect in Swift 2.0
您是否尝试使用界面构建器中的 UIButton(具有圆角)并尝试设置以使其看起来像标签。 如果您只想显示静态文本。
Did you try using the
UIButton
from the Interface builder (that has rounded corners) and experimenting with the settings to make it look like a label. if all you want is to display static text within.根据您正在做什么,您可以制作图像并以编程方式将其设置为背景。
Depending on what exactly you are doing you could make an image and set it as the background programatically.
iOS 3.0 及更高版本
iPhone OS 3.0 及更高版本支持
CALayer
类上的cornerRadius
属性。 每个视图都有一个可以操作的 CALayer 实例。 这意味着您可以在一行中获得圆角:您将需要
#import
并链接到 QuartzCore 框架才能访问 CALayer 的标头和属性。在 iOS 3.0 之前
,我最近使用的一种方法是创建一个 UIView 子类,它只绘制一个圆角矩形,然后创建 UILabel,或者在我的例子中,创建 UITextView,它内部的子视图。 具体来说:
UIView
子类并将其命名为RoundRectView
之类的名称。RoundRectView
的drawRect:
方法中,使用 Core Graphics 调用(例如用于边缘的 CGContextAddLineToPoint() 和用于圆角的 CGContextAddArcToPoint())在视图边界周围绘制一条路径角落。label.frame = CGRectInset(roundRectView.bounds, 8, 8);
)如果您创建通用 UIView,然后使用以下命令更改其类,则可以使用 Interface Builder 将 RoundRectView 放置在视图上检查员。 在编译并运行应用程序之前,您不会看到该矩形,但至少您可以放置子视图并将其连接到插座或操作(如果需要)。
iOS 3.0 and later
iPhone OS 3.0 and later supports the
cornerRadius
property on theCALayer
class. Every view has aCALayer
instance that you can manipulate. This means you can get rounded corners in one line:You will need to
#import <QuartzCore/QuartzCore.h>
and link to the QuartzCore framework to get access to CALayer's headers and properties.Before iOS 3.0
One way to do it, which I used recently, is to create a UIView subclass which simply draws a rounded rectangle, and then make the UILabel or, in my case, UITextView, a subview inside of it. Specifically:
UIView
subclass and name it something likeRoundRectView
.RoundRectView
'sdrawRect:
method, draw a path around the bounds of the view using Core Graphics calls like CGContextAddLineToPoint() for the edges and and CGContextAddArcToPoint() for the rounded corners.UILabel
instance and make it a subview of the RoundRectView.label.frame = CGRectInset(roundRectView.bounds, 8, 8);
)You can place the RoundRectView on a view using Interface Builder if you create a generic UIView and then change its class using the inspector. You won't see the rectangle until you compile and run your app, but at least you'll be able to place the subview and connect it to outlets or actions if needed.
对于 iOS 7.1 或更高版本的设备,您需要添加:
For devices with iOS 7.1 or later, you need to add:
对于基于 OScarsWyck 答案的 Swift IOS8 及以上版本:
For Swift IOS8 onwards based on OScarsWyck answer:
myLabel
的UILabel
。#import
在您的
viewDidLoad
中写入以下行:self .myLabel.layer.cornerRadius = 8;
祝你好运
UILabel
called:myLabel
.#import <QuartzCore/QuartzCore.h>
in your
viewDidLoad
write this line:self.myLabel.layer.cornerRadius = 8;
Good luck
您可以通过以下方式使任何控件的边框宽度为圆形边框:-
只需将
lblName
替换为您的UILabel
即可。注意:-不要忘记导入
You can make rounded border with width of border of any control in this way:-
Just replace
lblName
with yourUILabel
.Note:- Don't forget to import
<QuartzCore/QuartzCore.h>
Swift 3
如果您想要带有背景颜色的圆形标签,除了大多数其他答案之外,您还需要设置
图层
的背景颜色。 设置view
背景颜色时不起作用。如果您使用自动布局,想要标签周围有一些填充并且不想手动设置标签的大小,您可以创建 UILabel 子类并覆盖
intrinsincContentSize
属性:要将两者结合起来,您还可以需要设置
label.textAlignment = center
,否则文本将左对齐。Swift 3
If you want rounded label with background color, in addition to most of the other answers, you need to set
layer
's background color as well. It does not work when settingview
background color.If you are using auto layout, want some padding around the label and do not want to set the size of the label manually, you can create UILabel subclass and override
intrinsincContentSize
property:To combine the two you will also need to set
label.textAlignment = center
, otherwise the text would be left aligned.