RubyAttribute icon indicating copy to clipboard operation
RubyAttribute copied to clipboard

CTRubyAnnotation + AttributedString

RubyAttribute

CTRubyAnnotation + AttributedString

What is the cat doing here? with ruby annotations on the word cat

Why Display in UILabel + NSAttributedString

  1. Foundation/SwiftUI/UIKit/CoreText forgot to add kCTRubyAnnotationAttributeName to AttributedString (FB9953514)
  2. SwiftUI Text ignores custom attributes
  3. UITextView doesn't work because reasons

Note: remember adding including: \.coreText when constructing NSAttributedString or else it will ignore custom attributes as well.

Usage

Construct AttributedString directly:

var attributes = AttributeContainer()
attributes.rubyAnnotation = .makeRubyAnnotation(text: [.before: "ねこ"])
let attributedString = AttributedString("猫", attributes: attributes)

... or from markdown:

// defaults to before
try AttributedString(
    markdown: "^[猫](CTRubyAnnotation: 'ねこ')",
    including: \.coreText,
    options: .init(allowsExtendedAttributes: true)
)

// but you can specify where exactly, or even multiple positions
AttributedString(localized:
"""
The ^[猫](CTRubyAnnotation: {interCharacter: 'ㄇㄠ', after: 'cat'}) \
is seeking adoption.
""", including: \.coreText)