40

Is it possible to apply both stroke and fill with an NSAttributedString and a UILabel?

cacau
  • 3,606
  • 3
  • 21
  • 42
Piotr Tomasik
  • 9,074
  • 4
  • 44
  • 57

5 Answers5

99

Yes, the key is to apply a Negative value to the NSStrokeWidthAttributeName If this value is positive you will only see the stroke and not the fill.

Objective-C:

self.label.attributedText=[[NSAttributedString alloc] 
initWithString:@"string to both stroke and fill" 
attributes:@{
             NSStrokeWidthAttributeName: @-3.0,
             NSStrokeColorAttributeName:[UIColor yellowColor],
             NSForegroundColorAttributeName:[UIColor redColor]
             }
];

Thanks to @cacau below: See also Technical Q&A QA1531

Swift 4 version:

let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
                                                 .strokeColor: UIColor.yellow,
                                                 .foregroundColor: UIColor.red]

label.attributedText = NSAttributedString(string: text, attributes: attributes)

Swift 5.1 version:

let attributes: [NSAttributedString.Key : Any] = [.strokeWidth: -3.0,
                                                  .strokeColor: UIColor.yellow,
                                                  .foregroundColor: UIColor.red]

label.attributedText = NSAttributedString(string: text, attributes: attributes)
Ky -
  • 30,724
  • 51
  • 192
  • 308
Piotr Tomasik
  • 9,074
  • 4
  • 44
  • 57
8

Swift version:

let attributes = [NSStrokeWidthAttributeName: -3.0,
                      NSStrokeColorAttributeName: UIColor.yellowColor(),
                      NSForegroundColorAttributeName: UIColor.redColor()];

label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)

Swift 3 version:

    let attributes = [NSStrokeWidthAttributeName: -3.0,
                      NSStrokeColorAttributeName: UIColor.yellow,
                      NSForegroundColorAttributeName: UIColor.red] as [String : Any]

    label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes)
gvuksic
  • 2,983
  • 3
  • 32
  • 37
2

Now Latest in swift apple remove the [String: Any] for attributes key value declaration use as [NSAttributedStringKey : Any]

    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)

thank you

Hari Narayanan
  • 754
  • 1
  • 12
  • 36
2

Swift 4 version:

let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
                                                 .strokeColor: UIColor.yellow,
                                                 .foregroundColor: UIColor.red]

label.attributedText = NSAttributedString(string: text, attributes: attributes)
danfordham
  • 980
  • 9
  • 15
0

This is an example of Stroke and Oblique/tilt/slant text in UITextView

    public func setAttributedText(fontStyle: INSTextFontPickerStyle, strokeColor: UIColor = UIColor.white, foregroundColor: UIColor = UIColor.black, isObliqueness: Bool = false, isStrokes: Bool = false) {
        self.fontStyle = fontStyle

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = innterTextView.textAlignment

        // Tilting the text
        let obliquenessValue = isObliqueness ? NSNumber(value: 0.2) : NSNumber(value: 0) // The angle of tilting in clockwise

        // Stroke a line in text edges
        var strokeWidth = NSNumber(value: 0.0)
        var _strokeColor = UIColor.clear
        if isStrokes || fontStyle == .strokeEdges {
            strokeWidth = NSNumber(value: -2.0) // The width of stroke
            _strokeColor = strokeColor
        }
        
        textView.attributedText = NSAttributedString(string: textView.text, attributes: [
            NSAttributedString.Key.foregroundColor : foregroundColor,
            NSAttributedString.Key.font : UIFont.systemFont(ofSize: (actualFontSize > 0 ? actualFontSize : fontSize)),
            NSAttributedString.Key.paragraphStyle : paragraphStyle,
            NSAttributedString.Key.strokeColor : _strokeColor,
            NSAttributedString.Key.strokeWidth : strokeWidth,
            NSAttributedString.Key.obliqueness: obliquenessValue
        ])
    }
Johnny
  • 1,112
  • 1
  • 13
  • 21