alamodey
alamodey

Reputation: 14930

How to create ruby text in Swift?

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
  }
}

enter image description here

Upvotes: 1

Views: 590

Answers (1)

Chris Vasselli
Chris Vasselli

Reputation: 13344

I was able to get it working with two changes:

  1. Use a UILabel instead of a UITextView.
  2. Remove the 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.

食べ物 with furigana

Upvotes: 2

Related Questions