Help us understand the problem. What is going on with this article?

[swift] UIViewにhtmlを縦組みで表示

More than 3 years have passed since last update.

先日書いた「HTMLファイルを読み込んでUITextLabelに表示」から読み込んだデータをCoreTextを使用して縦組みで表示します。

secondViewController.swift
import CoreText
import UIKit

class secondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //UIViewで呼び出します。
        self.view = View()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    class View: UIView {
       var htmlText = "" //HTMLのテキスト初期化
        override func drawRect(rect: CGRect) {
            let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
                .UserDomainMask, true) as Array<String>
            let filePath = String(paths[0]) + "data.html"
            if let array = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? Array<String>{
                htmlText = array[0]
            }


            var height = 28.0
            var settings = [
                CTParagraphStyleSetting(
                    spec: .MinimumLineHeight,
                    valueSize: UInt(sizeofValue(height)),
                    value: &height)
            ]
            let style = CTParagraphStyleCreate(settings, UInt(settings.count))


            var err:NSError?
            var nsstringHtml = NSMutableAttributedString(
                data: htmlText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
                options: [ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType ],
                documentAttributes: nil,
                error: &err)

            let attributedText = NSMutableAttributedString(attributedString: nsstringHtml!)

           attributedText.addAttributes([
                NSFontAttributeName: UIFont(name: "HiraMinProN-W3", size: 14.0)!,
                NSVerticalGlyphFormAttributeName: true,
                kCTParagraphStyleAttributeName: style,
                ],
                range: NSMakeRange(0, attributedText.length))

            let context = UIGraphicsGetCurrentContext()

            CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0)
            CGContextAddRect(context, rect)
            CGContextFillPath(context)

            CGContextRotateCTM(context, CGFloat(M_PI_2))
            CGContextTranslateCTM(context, 30.0, 35.0)
            CGContextScaleCTM(context, 1.0, -1.0)

            let framesetter = CTFramesetterCreateWithAttributedString(attributedText)
            let path = CGPathCreateWithRect(CGRectMake(0.0, 0.0, rect.height, rect.width), nil)
            let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, nil)
            CTFrameDraw(frame, context)
        }
    }
}

しかし、これではすべての文字が同じフォントサイズで、同じフォントになってしまう、、、

-UILabel, UITextView, UITextField のテキストを NSAttributedString で装飾する
-How do you use NSAttributedString?
-NSAttributedStringを使ってみる
-SwiftでUILabelにHTMLを表示する方法
-ルビを振る

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away