Reputation: 14930
I'm trying to add ruby text annotation so I can display furigana on top of kanji words in a Japanese app. I believe CTRubyAnnotationCreateWithAttributes is the function to use to create ruby text but when I run this simple app it is only displaying the main text. Can someone point out what I am missing? Thanks.
import UIKit
class ViewController: UIViewController {
@IBOutlet var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let word = "食べ物"
let ruby = "たべもの"
var text = furigana(main: word, furigana: ruby)
textView.attributedText = text
}
func furigana(main: String, furigana: String) -> NSMutableAttributedString {
var ruby = furigana
var unmanage = Unmanaged.passRetained(ruby as CFString)
defer { unmanage.release() }
var text: [Unmanaged<CFString>?] = [unmanage, .none, .none, .none]
let rubyAttribute: [AnyHashable: Any] = [
kCTRubyAnnotationSizeFactorAttributeName: 0.5,
kCTForegroundColorAttributeName: UIColor.black
]
let annotation = CTRubyAnnotationCreateWithAttributes(.auto, .auto, .before, ruby as CFString, rubyAttribute as CFDictionary)
//let annotation = CTRubyAnnotationCreate(.center, .auto, 0.5, &text)
let attributed = NSMutableAttributedString(
string: main,
attributes: [kCTRubyAnnotationAttributeName as NSAttributedString.Key: annotation])
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.0
paragraphStyle.lineSpacing = 12
attributed.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributed.length))
attributed.addAttributes([NSAttributedString.Key.font: UIFont(name: "HiraMinProN-W3", size: 14.0)!, NSAttributedString.Key.verticalGlyphForm: false,],range: NSMakeRange(0, (attributed.length)))
return attributed
}
}
Upvotes: 1
Views: 590
Reputation: 13344
I was able to get it working with two changes:
UILabel
instead of a UITextView
.verticalGlyphForm
attribute.I'm not seeing any obvious documentation, but it appears that ruby text might not be supported on UITextView
. As for verticalGlyphForm
, the documentation says it's not supported on iOS and if specified the behavior is undefined.
Upvotes: 2