Skip to content

Instantly share code, notes, and snippets.

@macneko-ayu
Last active February 14, 2019 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macneko-ayu/1d175deb3fa961bb6d4bbabafe1e3566 to your computer and use it in GitHub Desktop.
Save macneko-ayu/1d175deb3fa961bb6d4bbabafe1e3566 to your computer and use it in GitHub Desktop.
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