Outline Uilabel Text in Uilabel Subclass

Outline UILabel text in UILabel Subclass

This code works for me.

Swift 3

let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.black,
NSForegroundColorAttributeName : UIColor.white,
NSStrokeWidthAttributeName : -4.0,
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
] as [String : Any]

myLabel.attributedText = NSMutableAttributedString(string: "Test me i have color.", attributes: strokeTextAttributes)

Output like this...





Swift 4.2 & 5.1

let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : UIColor.red,
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.strokeWidth : -4.0,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)]
as [NSAttributedString.Key : Any]

labelOutLine.attributedText = NSMutableAttributedString(string: "Your outline text", attributes: strokeTextAttributes)

Sample Image

How do I make UILabel display outlined text?

I was able to do it by overriding drawTextInRect:

- (void)drawTextInRect:(CGRect)rect {

CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 1);
CGContextSetLineJoin(c, kCGLineJoinRound);

CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];

CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];

self.shadowOffset = shadowOffset;

}

Outline for UILabel text

One option is to set the shadow, which might not be exactly what you want, but achieves a similar effect. You can manually adjust the offset:

UILabel *myLabel = ...;
lbl.shadowColor = [UIColor whiteColor];
lbl.shadowOffset = CGSizeMake(0, -1.0);

Please note that you can also define this in Interface Builder for your UILabel.

shadow http://i.minus.com/jbiG0jVdOxJbgh.png

If this is not enough for you check out this blog post which deals with subclassing UILabel to get a glow effect:

glow
(source: redrobotstudios.com)

How to add only outer outline to UILabel on iOS

You can subclass UILabel to get this effect

public class StrokedLabel: UILabel{
internal var mOutlineColor:UIColor?
internal var mOutlineWidth:CGFloat?

@IBInspectable var outlineColor: UIColor{
get { return mOutlineColor ?? UIColor.clear }
set { mOutlineColor = newValue }
}

@IBInspectable var outlineWidth: CGFloat{
get { return mOutlineWidth ?? 0 }
set { mOutlineWidth = newValue }
}

override public func drawText(in rect: CGRect) {
let shadowOffset = self.shadowOffset
let textColor = self.textColor

let c = UIGraphicsGetCurrentContext()
c?.setLineWidth(outlineWidth)
c?.setLineJoin(.round)
c?.setTextDrawingMode(.stroke)
self.textColor = mOutlineColor;
super.drawText(in:rect)

c?.setTextDrawingMode(.fill)
self.textColor = textColor
self.shadowOffset = CGSize(width: 0, height: 0)
super.drawText(in:rect)

self.shadowOffset = shadowOffset
}
}

Sample Image

How do I add an outline to my label in Swift?

You need to use NSAttributedString, set strokeColor and strokeWidth to set outline and foregroundColor to set text color. Try this:

let attrString = NSAttributedString(
string: "Write Something with Outline",
attributes: [
NSAttributedStringKey.strokeColor: UIColor.black,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.strokeWidth: -2.0,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)
]
)
yourLabel.attributedText = attrString

This will look like below:

Sample Image

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

iPhone SDK: How to add an outline to UILabel's text

Take a look at those two posts, I think they could help you:

How do I make UILabel display outlined text?

How do you stroke the outside of an NSAttributedString?

Overriding 'text' in UILabel

You can't redefine a property (var) from the superclass in your subclass. This was also true in obj-c, you could only redeclare read only properties to be read write. That's why you get an error.

So, instead, override the setter and getter functions and implement them to do what you want.



Related Topics



Leave a reply



Submit