Outline UILabel text in UILabel Subclass - ios

I'm trying hard to find a way to simply add an outline/stroke/contour to my UILabel text. Talking about a stroke around the letters of the text not around the background of a UILabel.
I'm using swift 3 and I'd like to outline my text directly into my subclass: UILabel.
I found multiple answers suggesting this way to do things :
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.black,
NSForegroundColorAttributeName : UIColor.white,
NSStrokeWidthAttributeName : -4.0,
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
]
self.attributedText = NSMutableAttributedString(string: self.text!, attributes: strokeTextAttributes)
But the thing is that it doesn't work. My text is still the same with no outline...
Could anyone help me here ?
That would be a great thing :)
Thanks a lot. Cheers guys.

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)
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)

#anandnimje answer converted to Swift 4.2 and wrapped it into a function:
public func stroke(font: UIFont, strokeWidth: Float, insideColor: UIColor, strokeColor: UIColor) -> [NSAttributedStringKey: Any]{
return [
NSAttributedStringKey.strokeColor : strokeColor,
NSAttributedStringKey.foregroundColor : insideColor,
NSAttributedStringKey.strokeWidth : -strokeWidth,
NSAttributedStringKey.font : font
]
}
Usage:
label.attributedText = NSMutableAttributedString(string: "Hello World",
attributes: stroke(font: UIFont(name: "SourceSansPro-Black", size: 20)!,
strokeWidth: 4, insideColor: .white, strokeColor: .black))
Make sure you have the right name for your UIFont, else it crashes. Should never be a problem if you have the right name.

Here you have class with implementation, copy and paste to playgrond for test:
class StrokedLabel: UILabel {
var strockedText: String = "" {
willSet(newValue) {
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.black,
NSForegroundColorAttributeName : UIColor.white,
NSStrokeWidthAttributeName : -4.0,
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
] as [String : Any]
let customizedText = NSMutableAttributedString(string: newValue,
attributes: strokeTextAttributes)
attributedText = customizedText
}
}
}
//////////// PLAYGROUND IMPLEMENTATION PART /////////
let text = "Stroked text"
// UILabel subclass initialization
let label = StrokedLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
// simple assign String to 'strockedText' property to see the results
label.strockedText = text
label.backgroundColor = UIColor.white
label
Swift 4.2
import UIKit
class StrokedLabel: UILabel {
var strockedText: String = "" {
willSet(newValue) {
let strokeTextAttributes : [NSAttributedString.Key : Any] = [
NSAttributedString.Key.strokeColor : UIColor.black,
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.strokeWidth : -4.0,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)
] as [NSAttributedString.Key : Any]
let customizedText = NSMutableAttributedString(string: newValue,
attributes: strokeTextAttributes)
attributedText = customizedText
}
}
}
//////////// PLAYGROUND IMPLEMENTATION PART /////////
let text = "Stroked text"
// UILabel subclass initialization
let label = StrokedLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
// simple assign String to 'strockedText' property to see the results
label.strockedText = text
label.backgroundColor = UIColor.clear
label
Maybe refactoring for this class will be welcomed, but should work for you at this form
As you can see usage is very convenient. 👾

Update to Swift 5
This answer is built on Anandnimje and J.Doe answers, and is meant to update and streamline it to make the usage clearer and simpler.
Simply use these two functions:
func outline(string:String, font:String, size:CGFloat, outlineSize:Float, textColor:UIColor, outlineColor:UIColor) -> NSMutableAttributedString {
return NSMutableAttributedString(string:string,
attributes: outlineAttributes(font: UIFont(name: font, size: size)!,
outlineSize: outlineSize, textColor: textColor, outlineColor: outlineColor))
}
func outlineAttributes(font: UIFont, outlineSize: Float, textColor: UIColor, outlineColor: UIColor) -> [NSAttributedString.Key: Any]{
return [
NSAttributedString.Key.strokeColor : outlineColor,
NSAttributedString.Key.foregroundColor : textColor,
NSAttributedString.Key.strokeWidth : -outlineSize,
NSAttributedString.Key.font : font
]
}
Then use outline with your labels as the following:
label.attributedText = outline(string: "Label Text", font: "HelveticaNeue", size: 14, outlineSize: 4, textColor: .white, outlineColor: .black)

Below is what I used in my App written in Swift 4.1
Swift 4.x
let strokeTextAttributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.strokeColor: UIColor.black,
NSAttributedStringKey.foregroundColor : UIColor.white,
NSAttributedStringKey.strokeWidth : -3.0,
NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
]

Your code works for me if I set the type of the attributes dictionary, like this:
let strokeTextAttributes: [String: Any] = [
// etc...
]
Maybe that's all your missing?

Related

Swift 5 Add alpha to UILabel with attributed text

I have a UILabel that incorporates two different strings/font via attributed text.
I would like to incorporate an alpha on the attrs2 of the UILabel. But leave the first part of the string with no alpha.
Code:
private let titleLabel: UILabel = {
let label = UILabel()
label.textColor = .black
label.textAlignment = .center
let attrs1 = [NSAttributedString.Key.font : UIFont(name:"SFMono-Bold", size: 20)]
let attrs2 = [NSAttributedString.Key.font : UIFont.sfMonoMedium(ofSize: 20)]
let attributedString1 = NSMutableAttributedString(string:"Fast credit card\n payments", attributes:attrs1 as [NSAttributedString.Key : Any])
let attributedString2 = NSMutableAttributedString(string:" using Square.", attributes:attrs2 as [NSAttributedString.Key : Any])
attributedString1.append(attributedString2)
label.attributedText = attributedString1
return label
}()
Pseudo:
let attrs2 = [NSAttributedString.Key.font : UIFont.sfMonoMedium(ofSize: 20), NSAttributedString.Key.font.alpha..... : 0.75]
or
attrs2.alpha = 0.75
You can do that with the .foregroundColor attribute.
let attrs2: [NSAttributedString.Key: Any] = [
.font : UIFont.sfMonoMedium(ofSize: 20),
.foregroundColor: UIColor.black.withAlphaComponent(0.5),
]

Swift: Refactoring NSMutableAttributedString

I have used NSMutableAttributedString/NSAttributedString here and there but don't have extensive experience with them. I have a code block that repeats itself and was wondering how would I go about refactoring it? I've been working on a few extensions to refactor this but haven't had any luck.
The attributes goes into a UILabel variable closure.
let attributes = NSMutableAttributedString(string: "ID: \n",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.backgroundColor : UIColor.clear,
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!])
attributes.append(NSMutableAttributedString(string: "\(nameID)",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.backgroundColor : UIColor.customBlue(),
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "\nDate Created: \n",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.backgroundColor : UIColor.clear,
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "TEST",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.backgroundColor : UIColor.customBlue(),
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "\nDate Last Used: \n",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.backgroundColor : UIColor.clear,
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "TEST",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.backgroundColor : UIColor.customBlue(),
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
Try looking at your code and seeing if there's a pattern. What I see is creating multiple NSAttributedStrings with String values, setting foreground and background, and the same font over and over. So this is ripe for the refactor picking.
First, set up your data:
// one note here is that you actually have a newline char for every entry, so it's probably better practice t simply drop them and apply them in your loop which we'll get to
let values = ["ID:", nameID /*I assume this is already a `String`*/, "Date Created: ", "TEST", "Date Last Used:", "Test"]
Colors seem to follow their own pattern: titles are black on clear and values are white on custom blue. We can use this in our loop, too. Let's set up our attributes so they're easy to reference:
let titleAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.backgroundColor: UIColor.clear,
.font: UIFont(name: "Helvetica", size: 15)!
]
let valueAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.white,
.backgroundColor: UIColor.customBlue(),
.font: UIFont(name: "Helvetica", size: 15)!
]
Now, let's loop:
let mutableString = NSMutableAttributedString()
for (index, currentValue) in values.enumerated() {
// if we're on an even number, we have a title. If it's odd, it's a value
let attributes = index % 2 == 0 ? titleAttributes : valueAttributes
// if we aren't on the last index, add a newline to the value
let value = index < values.count - 1 ? "\(currentValue)\n" : currentValue
mutableString.appendAttributedString(string: value, attributes: attributes)
}

How to add Stroke in UILabel Swift IOS

While add Stroke to UILabel App Crashes
It Crash due to found nil
let strokeTextAttributes = [
NSAttributedStringKey.strokeColor : UIColor.green,
NSAttributedStringKey.foregroundColor : UIColor.lightGray,
NSAttributedStringKey.strokeWidth : -4.0,
NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 52)
] as [NSAttributedStringKey : Any]
hello_cell_lb.attributedText = NSAttributedString(string: "\(hello_array[indexPath.row])", attributes: strokeTextAttributes)
You seem to add stroke to a nil label in cell but it works here

Text Shadow - iOS, swift 3.0

I'm trying to make shadow size a bit bigger but I can't do it.
so far:
findAPlace.titleLabel?.layer.shadowOffset = CGSize(width: -1, height: 1)
findAPlace.titleLabel?.layer.shouldRasterize = true
findAPlace.titleLabel?.layer.shadowRadius = 1
findAPlace.titleLabel?.layer.shadowOpacity = 1
findAPlace.titleLabel?.layer.shadowColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0).cgColor
how to scale shadow to be bigger than the text itself?
something like this.
Maybe with a border can be done, My text is the title of a UIButton!!!I expect it to be all around text of the uiButton
You can do in this way
Actually You need to use setTitleShadowColor instead of titleLabel?.layer.shadowColor
Here is full working code
let btnTemp = UIButton(type: .custom)
btnTemp.frame = CGRect(x: 50, y: 200, width: 150, height: 40)
btnTemp.setTitle("Hello", for: .normal)
btnTemp.titleLabel?.layer.shouldRasterize = true
btnTemp.titleLabel?.layer.shadowRadius = 1.0
btnTemp.titleLabel?.layer.shadowOpacity = 1.0
btnTemp.setTitleColor(UIColor.blue, for: .normal)
btnTemp.backgroundColor = UIColor.gray
btnTemp.titleLabel?.shadowOffset = CGSize(width: -1, height: 1)
btnTemp.setTitleShadowColor(UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0), for: .normal)
self.view.addSubview(btnTemp)
Hope it helps
Output:
You can follow this way to achieve the outlined text.
You have to use attributed string and use setAttributedTitle property of button to get required result.
Here is the code:
Swift 4
let strokeTextAttributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.strokeColor : UIColor.red,
NSAttributedStringKey.foregroundColor : UIColor.gray,
NSAttributedStringKey.strokeWidth : -2.0,
]
let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)
Swift 3
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.red,
NSForegroundColorAttributeName : UIColor.gray,
NSStrokeWidthAttributeName : -2.0,
] as [String : Any]
let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)
Output:

why multiline attributedString UITextView has a different line height?

I get different line height in textView with using same font
How to set fixed line height?
I have done a lot of attempts, any help is appreciated, thanks
set NSMutableParagraphStyle lineSpacing is useless
set lineHeightMultiple is to make the difference more obvious
[
demo
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 1000))
let data: [String] = [
"商品名称: 巧克力",
"商品名称: 巧克力",
"商品名称: 巧克力",
"注册未成功,请验证电子邮件",
"注册未成功,请验证电子邮件",
"注册未成功,请验证电子邮件",
"测试文字, 测试文字,测试文字",
"测试文字, 测试文字,测试文字",
"测试文字, 测试文字,测试文字",
]
let textView = UITextView(frame: view.frame)
let paragraphStyle = NSMutableParagraphStyle()
let bodyFont = UIFont.systemFont(ofSize: 20.0)
paragraphStyle.lineHeightMultiple = 4
var stripe = false
// attributedString
let mutableAttributedString = NSMutableAttributedString(string: "Test TextViewAttributedString\n", attributes: [
NSFontAttributeName: UIFont.systemFont(ofSize: 18.0)
])
for text: String in data {
var backgroundColor = UIColor(red:0.13, green:0.38, blue:0.95, alpha:1.00)
if stripe {
backgroundColor = UIColor(red:0.92, green:0.12, blue:0.38, alpha:1.00)
}
let contentAttributedString = NSAttributedString(string: text, attributes: [
NSBackgroundColorAttributeName: backgroundColor,
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: bodyFont
])
mutableAttributedString.append(contentAttributedString)
stripe = !stripe
// add newline character
let newlineAttributedString = NSAttributedString(string: "\n")
mutableAttributedString.append(newlineAttributedString)
}
textView.attributedText = mutableAttributedString
view.addSubview(textView)
PlaygroundPage.current.liveView = view
I found the reason, the newlineAttributedString also need NSFontAttributeName
let newlineAttributedString = NSAttributedString(string: "\n", attributes: [
NSFontAttributeName: bodyFont
])
mutableAttributedString.appendAttributedString(newlineAttributedString)

Resources