如何在iPhone上创建圆角UILabel?

new*_*ple 149 cocoa-touch objective-c rounded-corners uilabel ios

是否有内置的方法来创建圆角UILabels?如果答案是否定的,那么如何创建这样的对象呢?

ben*_*ado 227

iOS 3.0及更高版本

iPhone OS 3.0及更高版本支持该类的cornerRadius属性CALayer.每个视图都有一个CALayer可以操作的实例.这意味着您可以在一行中获得圆角:

view.layer.cornerRadius = 8;
Run Code Online (Sandbox Code Playgroud)

您将需要#import <QuartzCore/QuartzCore.h>并链接到QuartzCore框架以访问CALayer的标头和属性.

在iOS 3.0之前

我最近使用的一种方法是创建一个UIView子类,它只是绘制一个圆角矩形,然后制作UILabel,或者在我的例子中,UITextView,它内部的子视图.特别:

  1. 创建一个UIView子类并将其命名为RoundRectView.
  2. RoundRectViewdrawRect:方法,绘制路径周围使用核心图形的视图的边界调用等CGContextAddLineToPoint()的边缘和与CGContextAddArcToPoint()为圆角.
  3. 创建一个UILabel实例并使其成为RoundRectView的子视图.
  4. 将标签的框架设置为RoundRectView边界的几个像素插入.(例如,label.frame = CGRectInset(roundRectView.bounds, 8, 8);)

如果创建通用UIView,然后使用检查器更改其类,则可以使用Interface Builder将RoundRectView放置在视图上.在编译和运行应用程序之前,您不会看到矩形,但至少您可以放置​​子视图并将其连接到出口或操作(如果需要).

  • 如果不起作用,请添加[layer setMasksToBounds:YES]; (28认同)
  • 如果你想这样做在Interface Builder,而不是代码,你可以设置身份检查与关键路径"layer.cornerRadius"用户自定义运行属性. (20认同)

Osc*_*yck 138

对于iOS 7.1或更高版本的设备,您需要添加:

yourUILabel.layer.masksToBounds = YES;
yourUILabel.layer.cornerRadius = 8.0;
Run Code Online (Sandbox Code Playgroud)

  • 设置角半径但maskToBounds对于滚动/动画来说很慢.如果你设置图层的背景颜色与足够的视图以及7.1上的图层的cornerRadius(它将停止仅使用cornerRadius). (3认同)

VBa*_*thi 19

对于基于OScarsWyck的Swift IOS8以上答案:

yourUILabel.layer.masksToBounds = true
yourUILabel.layer.cornerRadius = 8.0
Run Code Online (Sandbox Code Playgroud)

  • 请不要将Stack Overflow与Objective-C的翻译混合到Swift中,特别是在这种情况下唯一的变化是将`YES`改为`true`时. (14认同)
  • 这种情况的变化很小,但作为一般意义,从Objective-C到Swift的翻译通常非常有用. (5认同)

Gab*_*iel 11

  1. 你有一个UILabel叫:myLabel.
  2. 在您的"m"或"h"文件导入中: #import <QuartzCore/QuartzCore.h>
  3. 在你viewDidLoad写这行:self.myLabel.layer.cornerRadius = 8;

    • 取决于你想如何将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>


Nik*_*iya 7

如果您希望故事板的 UI 对象(如 ( UILabel, UIView, UIButton, UIImageView) 的圆角,则设置clip to boundstrue 并将User Defined Runtime AttributesKey path设置为 layer.cornerRadius,type = Number 和 value = 9(根据您的要求)

将剪辑设置为边界 将用户定义的运行时属性设置为


Alp*_*sta 6

另一种方法是在UILabel后面放置一个png.我有几个标签的视图,这些标签覆盖了单个背景png,其中包含各个标签的所有图稿.


Bes*_*esi 6

我做了一个快速的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)


Ahm*_*lah 6

xCode 7.3.1 iOS 9.3.2

 _siteLabel.layer.masksToBounds = true;
  _siteLabel.layer.cornerRadius = 8;
Run Code Online (Sandbox Code Playgroud)


Mar*_*rný 5

斯威夫特 3

如果您想要带有背景颜色的圆形标签,除了大多数其他答案之外,您还需要设置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,否则文本将左对齐。


BHU*_*MAR 5

在 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”类中,该方法是用户定义的,但适用于应用程序中的所有标签,并且将有助于保持一致性和干净的代码,如果您将来更改任何样式只需要在扩展方法中。