NSAttributedString

class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding

This extension adds some useful functions to NSAttributedString.

  • UIFont or NSFont, default Helvetica(Neue) 12.

    Declaration

    Swift

    public func font(_ font: Font, range: NSRange? = nil) -> NSAttributedString

    Parameters

    font

    UIFont or NSFont, default Helvetica(Neue) 12.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • NSParagraphStyle, default defaultParagraphStyle

    Declaration

    Swift

    public func paragraphStyle(_ paragraphStyle: NSParagraphStyle, range: NSRange? = nil) -> NSAttributedString

    Parameters

    paragraphStyle

    NSParagraphStyle, default defaultParagraphStyle

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • UIColor or NSColor, default black.

    Declaration

    Swift

    public func foregroundColor(_ foregroundColor: Color, range: NSRange? = nil) -> NSAttributedString

    Parameters

    foregroundColor

    UIColor or NSColor, default black.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • UIColor or NSColor, default nil means no background.

    Declaration

    Swift

    public func backgroundColor(_ backgroundColor: Color, range: NSRange? = nil) -> NSAttributedString

    Parameters

    backgroundColor

    UIColor or NSColor, default nil means no background.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Default true means default ligatures, false means no ligatures.

    Declaration

    Swift

    public func ligature(_ ligature: Bool, range: NSRange? = nil) -> NSAttributedString

    Parameters

    ligature

    Default true means default ligatures, false means no ligatures.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Amount to modify default kerning. 0 means kerning is disabled.

    Declaration

    Swift

    public func kern(_ kern: Float, range: NSRange? = nil) -> NSAttributedString

    Parameters

    kern

    Amount to modify default kerning.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Default 0 means no strikethrough.

    Declaration

    Swift

    public func strikethroughStyle(_ strikethroughStyle: Int, range: NSRange? = nil) -> NSAttributedString

    Parameters

    strikethroughStyle

    Default 0 means no strikethrough.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • NSUnderlineStyle.

    Declaration

    Swift

    public func underlineStyle(_ underlineStyle: NSUnderlineStyle, range: NSRange? = nil) -> NSAttributedString

    Parameters

    underlineStyle

    NSUnderlineStyle.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • UIColor or NSColor, default nil means same as foreground color.

    Declaration

    Swift

    public func strokeColor(_ strokeColor: Color, range: NSRange? = nil) -> NSAttributedString

    Parameters

    strokeColor

    UIColor or NSColor, default nil means same as foreground color.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • In percent of font point size, default 0 measn no stroke. Positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0).

    Declaration

    Swift

    public func strokeWidth(_ strokeWidth: Float, range: NSRange? = nil) -> NSAttributedString

    Parameters

    strokeWidth

    In percent of font point size, default 0 measn no stroke.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • NSShadow, default nil means no shadow.

    Declaration

    Swift

    public func shadow(_ shadow: NSShadow, range: NSRange? = nil) -> NSAttributedString

    Parameters

    shadow

    NSShadow, default nil means no shadow.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Default nil means no text effect.

    Declaration

    Swift

    public func textEffect(_ textEffect: String, range: NSRange? = nil) -> NSAttributedString

    Parameters

    textEffect

    Default is nil means no text effect. Currently, only NSTextEffectLetterpressStyle can be used.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • NSTextAttachment, default is nil.

    Declaration

    Swift

    public func attachment(_ attachment: NSTextAttachment, range: NSRange? = nil) -> NSAttributedString

    Parameters

    attachment

    NSTextAttachment, default is nil.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • NSURL.

    Declaration

    Swift

    public func link(_ link: NSURL, range: NSRange? = nil) -> NSAttributedString

    Parameters

    link

    NSURL.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Offset from baseline, default is 0.

    Declaration

    Swift

    public func baselineOffset(_ baselineOffset: Float, range: NSRange? = nil) -> NSAttributedString

    Parameters

    baselineOffset

    Offset from baseline, default is 0.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • UIColor or NSColor, default nil means same as foreground color.

    Declaration

    Swift

    public func underlineColor(_ underlineColor: Color, range: NSRange? = nil) -> NSAttributedString

    Parameters

    underlineColor

    UIColor or NSColor, default nil means same as foreground color.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • UIColor or NSColor, default nil means same as foreground color.

    Declaration

    Swift

    public func strikethroughColor(_ strikethroughColor: Color, range: NSRange? = nil) -> NSAttributedString

    Parameters

    strikethroughColor

    UIColor or NSColor, default nil means same as foreground color.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Skew to be applied to glyphs, default 0 means no skew.

    Declaration

    Swift

    public func obliqueness(_ obliqueness: Float, range: NSRange? = nil) -> NSAttributedString

    Parameters

    obliqueness

    Skew to be applied to glyphs, default 0 means no skew.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Log of expansion factor to be applied to glyphs, default 0 means no expansion.

    Declaration

    Swift

    public func expansion(_ expansion: Float, range: NSRange? = nil) -> NSAttributedString

    Parameters

    expansion

    Log of expansion factor to be applied to glyphs, default 0 means no expansion.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Array of Int representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values. Remeber to use .rawValue, because the attribute wants an Int.

    • LRE: NSWritingDirection.leftToRight, NSWritingDirectionFormatType.embedding.
    • RLE: NSWritingDirection.rightToLeft, NSWritingDirectionFormatType.embedding.
    • LRO: NSWritingDirection.leftToRight, NSWritingDirectionFormatType.override.
    • RLO: NSWritingDirection.rightToLeft, NSWritingDirectionFormatType.override.

    Declaration

    Swift

    public func writingDirection(_ writingDirection: [Int], range: NSRange? = nil) -> NSAttributedString

    Parameters

    writingDirection

    Array of Int representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • The value 0 indicates horizontal text, the value 1 indicates vertical text. In iOS, horizontal text is always used and specifying a different value is undefined.

    Declaration

    Swift

    public func verticalGlyphForm(_ verticalGlyphForm: Bool, range: NSRange? = nil) -> NSAttributedString

    Parameters

    verticalGlyphForm

    The value false indicates horizontal text, the value true indicates vertical text.

    range

    Application range. Default is all the String.

    Return Value

    Returns a NSAttributedString.

  • Set text alignment to left.

    Declaration

    Swift

    public func textAlignmentLeft() -> NSAttributedString

    Return Value

    Returns a NSAttributedString.

  • Set text alignment to right.

    Declaration

    Swift

    public func textAlignmentRight() -> NSAttributedString

    Return Value

    Returns a NSAttributedString.

  • Set text alignment to center.

    Declaration

    Swift

    public func textAlignmentCenter() -> NSAttributedString

    Return Value

    Returns a NSAttributedString.

  • Set text alignment to justified.

    Declaration

    Swift

    public func textAlignmentJustified() -> NSAttributedString

    Return Value

    Returns a NSAttributedString.

  • Returns a list of all string attributes.

    Declaration

    Swift

    public func attributes() -> [NSAttributedString.Key : Any]

    Return Value

    Returns a list of all string attributes.