Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UILabelの文字列に線をつけて袋文字にするextension
// Please, paste the code in playground and execute it.
import UIKit
extension UILabel{
/// makeOutLine
///
/// - Parameters:
/// - strokeWidth: 線の太さ。負数
/// - oulineColor: 線の色
/// - foregroundColor: 縁取りの中の色
func makeOutLine(strokeWidth: CGFloat, oulineColor: UIColor, foregroundColor: UIColor) {
let strokeTextAttributes = [
.strokeColor : oulineColor,
.foregroundColor : foregroundColor,
.strokeWidth : strokeWidth,
.font : self.font
] as [NSAttributedString.Key : Any]
self.attributedText = NSMutableAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
}
}
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 500, height: 60))
label.font = UIFont.boldSystemFont(ofSize: 50)
label.text = "我が家の猫は7.4kg"
label.makeOutLine(strokeWidth: -2.0, oulineColor: .white, foregroundColor: .blue)
import UIKit
extension UILabel{
/// makeOutLine
///
/// - Parameters:
/// - strokeWidth: 線の太さ。負数
/// - oulineColor: 線の色
/// - foregroundColor: 縁取りの中の色
func makeOutLine(strokeWidth: CGFloat, oulineColor: UIColor, foregroundColor: UIColor) {
let strokeTextAttributes = [
.strokeColor : oulineColor,
.foregroundColor : foregroundColor,
.strokeWidth : strokeWidth,
.font : self.font
] as [NSAttributedString.Key : Any]
self.attributedText = NSMutableAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.