CTRubyAnnotation + AttributedString
- Foundation/SwiftUI/UIKit/CoreText forgot to add
kCTRubyAnnotationAttributeName
toAttributedString
(FB9953514) - SwiftUI
Text
ignores custom attributes UITextView
doesn't work because reasons
Note: remember adding
including: \.coreText
when constructingNSAttributedString
or else it will ignore custom attributes as well.
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)