new*_*ple 149 cocoa-touch objective-c rounded-corners uilabel ios
是否有内置的方法来创建圆角UILabels?如果答案是否定的,那么如何创建这样的对象呢?
ben*_*ado 227
iPhone OS 3.0及更高版本支持该类的cornerRadius属性CALayer.每个视图都有一个CALayer可以操作的实例.这意味着您可以在一行中获得圆角:
view.layer.cornerRadius = 8;
Run Code Online (Sandbox Code Playgroud)
您将需要#import <QuartzCore/QuartzCore.h>并链接到QuartzCore框架以访问CALayer的标头和属性.
我最近使用的一种方法是创建一个UIView子类,它只是绘制一个圆角矩形,然后制作UILabel,或者在我的例子中,UITextView,它内部的子视图.特别:
UIView子类并将其命名为RoundRectView.RoundRectView的drawRect:方法,绘制路径周围使用核心图形的视图的边界调用等CGContextAddLineToPoint()的边缘和与CGContextAddArcToPoint()为圆角.UILabel实例并使其成为RoundRectView的子视图.label.frame = CGRectInset(roundRectView.bounds, 8, 8);)如果创建通用UIView,然后使用检查器更改其类,则可以使用Interface Builder将RoundRectView放置在视图上.在编译和运行应用程序之前,您不会看到矩形,但至少您可以放置子视图并将其连接到出口或操作(如果需要).
Osc*_*yck 138
对于iOS 7.1或更高版本的设备,您需要添加:
yourUILabel.layer.masksToBounds = YES;
yourUILabel.layer.cornerRadius = 8.0;
Run Code Online (Sandbox Code Playgroud)
VBa*_*thi 19
对于基于OScarsWyck的Swift IOS8以上答案:
yourUILabel.layer.masksToBounds = true
yourUILabel.layer.cornerRadius = 8.0
Run Code Online (Sandbox Code Playgroud)
Gab*_*iel 11
UILabel叫:myLabel.#import <QuartzCore/QuartzCore.h>在你viewDidLoad写这行:self.myLabel.layer.cornerRadius = 8;
祝好运
Piy*_*bey 11
您可以通过以下方式使用任何控件的边框宽度制作圆角边框: -
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]];
Run Code Online (Sandbox Code Playgroud)
只需替换lblName你的UILabel.
注意: -别忘了导入<QuartzCore/QuartzCore.h>
如果您希望故事板的 UI 对象(如 ( UILabel, UIView, UIButton, UIImageView) 的圆角,则设置clip to boundstrue 并将User Defined Runtime AttributesKey path设置为
layer.cornerRadius,type = Number 和 value = 9(根据您的要求)
我做了一个快速的UILabel子类来实现这个效果.此外,我自动将文本颜色设置为黑色或白色以获得最大对比度.
二手SO-Posts:
只需将其粘贴到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://stackoverflow.com/a/33015915/784318
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://stackoverflow.com/a/15184257/784318
frame = CGRectInset(self.frame, -borderWidth, -borderWidth)
}
}
extension UIColor {
// Credits to https://stackoverflow.com/a/29044899/784318
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())
Run Code Online (Sandbox Code Playgroud)
xCode 7.3.1 iOS 9.3.2
_siteLabel.layer.masksToBounds = true;
_siteLabel.layer.cornerRadius = 8;
Run Code Online (Sandbox Code Playgroud)
如果您想要带有背景颜色的圆形标签,除了大多数其他答案之外,您还需要设置layer的背景颜色。设置view背景颜色时不起作用。
label.layer.cornerRadius = 8
label.layer.masksToBounds = true
label.layer.backgroundColor = UIColor.lightGray.cgColor
Run Code Online (Sandbox Code Playgroud)
如果您使用自动布局,希望在标签周围有一些填充并且不想手动设置标签的大小,您可以创建 UILabel 子类并覆盖intrinsincContentSize属性:
class LabelWithPadding: UILabel {
override var intrinsicContentSize: CGSize {
let defaultSize = super.intrinsicContentSize
return CGSize(width: defaultSize.width + 12, height: defaultSize.height + 8)
}
}
Run Code Online (Sandbox Code Playgroud)
要将两者结合起来,您还需要设置label.textAlignment = center,否则文本将左对齐。
在 Xcode 8.1.2 和 Swift 3 中运行良好
.cornerRadius是设置圆角边缘的关键属性。如果您对应用程序中的所有标签使用相同的样式,我建议您使用扩展方法。
代码:
// extension Class
extension UILabel {
// extension user defined Method
func setRoundEdge() {
let myGreenColor = (UIColor(red: -0.108958, green: 0.714926, blue: 0.758113, alpha: 1.0))
//Width of border
self.layer.borderWidth = 1.0
//How much the edge to be rounded
self.layer.cornerRadius = 5.0
// following properties are optional
//color for border
self.layer.borderColor = myGreenColor.cgColor
//color for text
self.textColor = UIColor.red
// Mask the bound
self.layer.masksToBounds = true
//clip the pixel contents
self.clipsToBounds = true
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
为什么选择扩展方法?
创建一个 Swift 文件并添加以下代码,其中将 Extention 方法添加到“UILabel”类中,该方法是用户定义的,但适用于应用程序中的所有标签,并且将有助于保持一致性和干净的代码,如果您将来更改任何样式只需要在扩展方法中。
| 归档时间: |
|
| 查看次数: |
126322 次 |
| 最近记录: |