Как нарисовать вертикальный текст на CGContext? - PullRequest
0 голосов
/ 02 мая 2020

Я хочу нарисовать вертикальный текст. Я пробовал другой способ повернуть строку, используя Поворот на , но он не работает

Вот мой код

override func draw(with box: PDFDisplayBox, to context: CGContext) {

    let name = "Sunny"
    // Draw original content
    super.draw(with: box, to: context)

    // Draw rotated overlay string
    UIGraphicsPushContext(context)
    context.saveGState()

    let pageBounds = self.bounds(for: box)
    context.translateBy(x: pageBounds.size.width, y: 0)
    context.scaleBy(x: -1.0, y: -1.0)
    context.rotate(by: CGFloat.pi / 4.0)

    let string: NSString = name as! NSString
    let attributes = [
        NSAttributedString.Key.foregroundColor: UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5),
        NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 64)
    ]

    string.draw(at: CGPoint(x:250, y:40), withAttributes: attributes)

    context.restoreGState()
    UIGraphicsPopContext()

}

Как я могу нарисовать?

1 Ответ

1 голос
/ 03 мая 2020

Мы можем нарисовать вертикальную строку, используя приведенный ниже код, в который я преобразовал данную строку, добавив line separators между символами.

 override func draw(with box: PDFDisplayBox, to context: CGContext) {

        // Draw original content
        super.draw(with: box, to: context)

        // Draw rotated overlay string
        UIGraphicsPushContext(context)
        context.saveGState()

        let pageBounds = self.bounds(for: box)
        context.translateBy(x: 0.0, y: pageBounds.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.rotate(by: CGFloat.pi / -60.0)

        // Convert string by adding line separators.
        let string = "Sunny".trimmingCharacters(in: .whitespaces)
        let asArray = Array(string)
        let verticalString = asArray.map { "\($0)" }.joined(separator: "\n")

        let attributes: [NSAttributedString.Key: Any] = [
            NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.4980392157, green: 0.4980392157, blue: 0.4980392157, alpha: 0.5),
            NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 64)
        ]

        verticalString.draw(at: CGPoint(x: 250, y: 40), withAttributes: attributes)

        context.restoreGState()
        UIGraphicsPopContext()

    }

Вывод вертикальной строки в PDF

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...