https://github.com/apollozhu/rubyattribute
CTRubyAnnotation + AttributedString
https://github.com/apollozhu/rubyattribute
Last synced: about 1 year ago
JSON representation
CTRubyAnnotation + AttributedString
- Host: GitHub
- URL: https://github.com/apollozhu/rubyattribute
- Owner: ApolloZhu
- License: unlicense
- Created: 2022-03-11T09:15:11.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-11T10:29:07.000Z (over 4 years ago)
- Last Synced: 2025-03-24T01:14:14.810Z (about 1 year ago)
- Language: Swift
- Size: 12.7 KB
- Stars: 26
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RubyAttribute
CTRubyAnnotation + AttributedString

## Why Display in UILabel + NSAttributedString
0. Foundation/SwiftUI/UIKit/CoreText forgot to add `kCTRubyAnnotationAttributeName` to `AttributedString` (FB9953514)
1. SwiftUI `Text` ignores custom attributes
2. `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:
```swift
var attributes = AttributeContainer()
attributes.rubyAnnotation = .makeRubyAnnotation(text: [.before: "ねこ"])
let attributedString = AttributedString("猫", attributes: attributes)
```
... or from markdown:
```swift
// 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)
```