有没有内置的方法可以创建圆角UILabel? 如果答案是否定的,那么怎样才能创建这样一个对象呢?
有没有内置的方法可以创建圆角UILabel? 如果答案是否定的,那么怎样才能创建这样一个对象呢?
iPhone OS 3.0 及以上版本支持 CALayer
类的 cornerRadius
属性。每个视图都有一个 CALayer
实例,您可以对其进行操作。这意味着您可以通过一行代码获得圆角:
view.layer.cornerRadius = 8;
您需要#import <QuartzCore/QuartzCore.h>
并链接到QuartzCore框架,以访问CALayer的头文件和属性。label.frame = CGRectInset(roundRectView.bounds, 8, 8);
)对于iOS 7.1或更高版本的设备,您需要添加:
yourUILabel.layer.masksToBounds = YES;
yourUILabel.layer.cornerRadius = 8.0;
根据 OScarsWyck 的回答,适用于 Swift IOS8 及以上版本:
yourUILabel.layer.masksToBounds = true
yourUILabel.layer.cornerRadius = 8.0
myLabel
的 UILabel
。#import <QuartzCore/QuartzCore.h>
在你的 viewDidLoad
中写入这行代码:self.myLabel.layer.cornerRadius = 8;
祝好运
您可以通过以下方式使任何控件的边框变为圆角:
border-radius: 50%;
其中,边框宽度可以根据需要进行调整。
CALayer * l1 = [lblName layer];
[l1 setMasksToBounds:YES];
[l1 setCornerRadius:5.0];
// You can even add a border
[l1 setBorderWidth:5.0];
[l1 setBorderColor:[[UIColor darkGrayColor] CGColor]];
只需用您的UILabel
替换lblName
即可。
注意:不要忘记导入<QuartzCore/QuartzCore.h>
如果你想要一个带背景颜色的圆角标签,除了其他答案之外,你还需要设置layer
的背景颜色。在设置view
的背景颜色时是不起作用的。
label.layer.cornerRadius = 8
label.layer.masksToBounds = true
label.layer.backgroundColor = UIColor.lightGray.cgColor
如果您正在使用自动布局,希望在标签周围设置一些填充,并且不想手动设置标签的大小,那么您可以创建一个UILabel子类并覆盖intrinsicContentSize
属性:
class LabelWithPadding: UILabel {
override var intrinsicContentSize: CGSize {
let defaultSize = super.intrinsicContentSize
return CGSize(width: defaultSize.width + 12, height: defaultSize.height + 8)
}
}
label.textAlignment = center
,否则文本将左对齐。UILabel
子类来实现这个效果。此外,我自动将文本颜色设置为黑色或白色,以获得最大对比度。
使用的SO帖子:
只需将此粘贴到iOS Playground中:
//: Playground - noun: a place where people can play
import UIKit
class PillLabel : UILabel{
@IBInspectable var color = UIColor.lightGrayColor()
@IBInspectable var cornerRadius: CGFloat = 8
@IBInspectable var labelText: String = "None"
@IBInspectable var fontSize: CGFloat = 10.5
// This has to be balanced with the number of spaces prefixed to the text
let borderWidth: CGFloat = 3
init(text: String, color: UIColor = UIColor.lightGrayColor()) {
super.init(frame: CGRectMake(0, 0, 1, 1))
labelText = text
self.color = color
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
// This has to be balanced with the borderWidth property
text = " \(labelText)".uppercaseString
// Credits to https://dev59.com/AXE95IYBdhLWcg3wbtfO#33015915
layer.borderWidth = borderWidth
layer.cornerRadius = cornerRadius
backgroundColor = color
layer.borderColor = color.CGColor
layer.masksToBounds = true
font = UIFont.boldSystemFontOfSize(fontSize)
textColor = color.contrastColor
sizeToFit()
// Credits to https://dev59.com/NWUp5IYBdhLWcg3wkITF#15184257
frame = CGRectInset(self.frame, -borderWidth, -borderWidth)
}
}
extension UIColor {
// Credits to https://dev59.com/bXE85IYBdhLWcg3w_I78#29044899
func isLight() -> Bool{
var green: CGFloat = 0.0, red: CGFloat = 0.0, blue: CGFloat = 0.0, alpha: CGFloat = 0.0
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
let brightness = ((red * 299) + (green * 587) + (blue * 114) ) / 1000
return brightness < 0.5 ? false : true
}
var contrastColor: UIColor{
return self.isLight() ? UIColor.blackColor() : UIColor.whiteColor()
}
}
var label = PillLabel(text: "yellow", color: .yellowColor())
label = PillLabel(text: "green", color: .greenColor())
label = PillLabel(text: "white", color: .whiteColor())
label = PillLabel(text: "black", color: .blackColor())
xCode 7.3.1 iOS 9.3.2
_siteLabel.layer.masksToBounds = true;
_siteLabel.layer.cornerRadius = 8;
另一种方法是在UILabel后面放置一个png。我有一些视图,其中有几个标签覆盖在单个背景png上,该png具有各个标签的所有艺术品。