How to Create a Round Cornered Uilabel on the Iphone

How do I create a round cornered UILabel on the iPhone?

iOS 3.0 and later

iPhone OS 3.0 and later supports the cornerRadius property on the CALayer class. Every view has a CALayer instance that you can manipulate. This means you can get rounded corners in one line:

view.layer.cornerRadius = 8;

You will need to #import 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:

  1. Create a UIView subclass and name it something like RoundRectView.
  2. In RoundRectView's drawRect: 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.
  3. Create a UILabel instance and make it a subview of the RoundRectView.
  4. Set the frame of the label to be a few pixels inset of the RoundRectView's bounds. (For example, 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.

How to round edges of UILabel with Swift

Assuming you have added a backgroundColor to your label otherwise there would be no way to tell if it had edges, you can use QuartzCore to round the edges of a label.

import QuartzCore

yourLabel.layer.backgroundColor = UIColor.redColor().CGColor
yourLabel.layer.cornerRadius = 5
yourLabel.layer.masksToBounds = true

How to use rounded corner label in iphone, UILabel Round Corners

Hard to know for sure what you're asking as you didn't include the errors you're getting. Have you added the QuartzCore framework to your project and #import to the file modifying the layer? If that's not it, add the errors and more info to your question.

EDIT: you can also #import as suggested in the comments. QuartzCore.h includes CALayer.h along with the rest of the QuartzCore components.

How do I round only the top two corners of a UILabel?

You can do this using CALayers and masks

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskPath.CGPath;
label.layer.mask = maskLayer;

where maskPath is a UIBezierPath set up using bezierPathWithRoundedRect:byRoundingCorners:cornerRadii

Rounded corners of UILabel swift

I think you should set maskToBounds for textLabel. try this:

textLabel?.layer.masksToBounds = true

UILabel rounding corners cuts-off text

You're doing it too early.

You set the cellTypeToDraw and thus call your roundCorners(corners:, radius:) method at a point in time when the layout isn't done yet. In cellForRow(at:) you get a recycled cell whose chatLabel still has the frame that matched the text it displayed previously.

A quick fix would be to move this corner-rounding to the cell's layoutSubviews() method. Something like:

override func layoutSubviews() {
super.layoutSubviews()
chatLabel.roundCorners(corners: [.topLeft,.bottomLeft,.bottomRight], radius: 7.0)
}

(You can check the cellTypeToDraw there as well if you only want to have rounded corners for some cell types.)


By the way:

Tip 1:

You should never do something like that:

var cellTypeToDraw:cellType {
get {
return self.cellTypeToDraw
}
// ...
}

This will result in an endless recursion when you ever try to call cellTypeToDraw and eventually crash your app. By implementing the getter and the setter you define a computed property that is not backed by an instance variable. self.cellTypeToDraw doesn't have a value. It simply calls the getter again which returns self.cellTypeToDraw again and so on.

Solution: Don't use a computed property but a "regular" one which is backed by an instance variable and do all your cell setup in the property's observer:

var cellTypeToDraw:cellType {
didSet {
// configure your cell here
}
}

Tip 2:

You can (and should) omit all the self. prefixes in Swift for better readability and less work.

Corner are not round of UILabel and UIbutton ios swift

That happens because bounds of these labels and buttons are most probably different when you call rounded...() methods and when they are presented in a table view.

That's because the bounds value that you use to create the UIBezierPath is different from one that these views have after they are presented in the cell. Table view layouts a cell and if the CAShapeLayer you create exceeds its bounds, it gets "cut off".

You should create a subclasses of UIButton and UILabel that would update the bezier path of their masks every time they layout.

class RoundedLabel: UILabel {

override func layoutSubviews() {
super.layoutSubviews()

let maskPath = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight, .bottomRight, .bottomLeft],
cornerRadii: CGSize(width: 10, height: 10))

let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.cornerRadius = 5
maskLayer.masksToBounds = true
maskLayer.path = maskPath.cgPath
layer.mask = maskLayer
}
}

As further optimization you could lazily initialize the mask layer (meaning it would be created when it's accessed for the first time) and only change it's frame and path in the layoutSubviews method.

class RoundedLabel: UILabel {

private lazy var maskLayer: CAShapeLayer = {
let maskLayer = CAShapeLayer()
maskLayer.cornerRadius = 5
maskLayer.masksToBounds = true
self.layer.mask = maskLayer

return maskLayer
}()

override func layoutSubviews() {
super.layoutSubviews()

let maskPath = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight, .bottomRight, .bottomLeft],
cornerRadii: CGSize(width: 10, height: 10))

maskLayer.path = maskPath.cgPath
maskLayer.frame = bounds
}
}


Related Topics



Leave a reply



Submit