How to Draw Border Around a Uilabel

How to draw border around a UILabel?

You can set label's border via its underlying CALayer property:

#import <QuartzCore/QuartzCore.h>

myLabel.layer.borderColor = [UIColor greenColor].CGColor
myLabel.layer.borderWidth = 3.0

Swift 5:

myLabel.layer.borderColor = UIColor.darkGray.cgColor
myLabel.layer.borderWidth = 3.0

draw only top, right, and bottom border around a uilabel

You can use a mask. This is the code I used to test the theory, and it works well:

// Define the border width in a variable, we'll be using it elsewhere
CGFloat borderWidth = 1.0;

// This creates a testing view to test the theory, in your case this will be your UILabel
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 250, 100)];
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = borderWidth;
[self.view addSubview:view];

// Create the mask to cover the area of the view you want to **show**
// Here, we create a mask that covers most of the view, except the left edge
// The mask needs to be coloured in black, as black acts as transparent, whereas white is opaque in mask parlance
UIView* mask = [[UIView alloc] initWithFrame:CGRectMake(borderWidth, 0, view.frame.size.width - borderWidth, view.frame.size.height)];
mask.backgroundColor = [UIColor blackColor];
view.layer.mask = mask.layer;

You can adjust the size and position of the mask (given the borderWidth) to show/hide the border edges you're interested in. The example above hides the left edge.

iOS: Draw border around text UILabel

The problem is quite hard to solve for UILabel, because you have no direct access to NSLayoutManager, which is key for my solution.

I have created IBDesignable UILabel subclass LineHighlightedLabel, which can do the job. The visual is not quite the same as image you provided but you can get there.

Important part is to set text to UILabel as NSAttributedString, not just plain text.

@IBDesignable
public class LineHighlightedLabel: UILabel {
public override func draw(_ rect: CGRect) {
let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage.init(attributedString: attributedText!)
textStorage.addLayoutManager(layoutManager)

let textContainer = NSTextContainer.init(size: bounds.size)
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = numberOfLines
textContainer.lineBreakMode = lineBreakMode

layoutManager.addTextContainer(textContainer)

layoutManager.enumerateLineFragments(forGlyphRange: NSMakeRange(0, textStorage.length)) { (rect, usedRect, textContainer, glyphRange, bool) in
let lineRect = CGRect(x: usedRect.origin.x, y: usedRect.origin.y + 1, width: usedRect.size.width, height: usedRect.size.height - 2)
UIColor.green.setStroke()
let lineRectPath = UIBezierPath(roundedRect: lineRect, cornerRadius: 5)
lineRectPath.lineWidth = 0.5
lineRectPath.stroke()
}

super.drawText(in: rect)
}

public override func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}
}

LineHighlightedLabel is providing nice preview in interface builder, you can play with values easily.

Sample Image

Drawing border in UILabel

Looking in CALayer.h, we see that the default value for borderWidth is 0.0.

/* The width of the layer's border, inset from the layer bounds. The
* border is composited above the layer's content and sublayers and
* includes the effects of the `cornerRadius' property. Defaults to
* zero. Animatable. */

@property CGFloat borderWidth;

In order for the border to appear, you must set the borderWidth to something greater than zero.

You can set the background color on the label directly as so:

label.backgroundColor = [UIColor cyanColor];

To set a border, you need to set the width, which can be done like so:

label.layer.borderWidth = 2.0f;

Once you have set a border width, to set the color of the border on the label, you're setting the view's layer's border, which uses a CGColor, so you'll have to do this:

label.layer.borderColor = [UIColor redColor].CGColor;

And if you want to round the corners, you can add this:

label.layer.cornerRadius = 10.0f;

You don't need to set the background color. You really need to only set the borderWidth and borderColor.

Setting custom border using sublass of UILabel in swift

Change your function like this.

func setBottomBorder(){

let borderWidth:CGFloat = 4.0 //Change this according to your needs
let lineView = UIView.init(frame: CGRect.init(x: 0, y:self.frame.size.height - borderWidth , width: self.frame.size.width, height: borderWidth))
lineView.backgroundColor = UIColor.green
self.addSubview(lineView)

}

From your attribute inspector, don't forget to change class like this.

Sample Image

output:

Sample Image

How to draw a border around a paragraph in UILabel?

I'm not aware of this being implemented in the SDK, even if you can restrict to iOS 7. There are at least two general options though.

In a subclass of UILabel you can use the sizing methods of NSAttributedString, such as boundingRectWithSize:options:context: and size, to calculate where the subtext is to appear, and in an override of drawTextInRect: draw the border. You might deduce the required border frame by calculating the frame for the preceding text and for the subtext appended and then taking their difference.

Another option is to set custom attributes on your NSAttributedStrings, something Apple openly encourages, from Apple's overview:

You can assign any attribute name/value pair you wish to a range of
characters—it is up to your application to interpret custom attributes
(see Attributed String Programming Guide).

Then in a subclass of NSAttributedStrings, override the drawing methods such as drawInRect: and implement similar custom drawing logic per the first suggestion for your custom attribute, otherwise rely on super.



Related Topics



Leave a reply



Submit