如何使用Swift旋转核心文本?

时间:2016-02-13 13:14:21

标签: ios swift macos rotation core-graphics

我正在尝试使用CoreGraphics制作模拟时钟。

我的问题是我不知道如何旋转数字文本。

代码的一般过程如下:

首先我旋转上下文,然后,如果适用,我画一个数字;重复。

完整的游乐场

import UIKit
import Foundation
import XCPlayground

class ClockFace: UIView {
    func drawText(context: CGContextRef, text: NSString, attributes: [String: AnyObject], x: CGFloat, y: CGFloat) -> CGSize {
        CGContextTranslateCTM(context, 0, 0)
        CGContextScaleCTM(context, 1, -1)
        let font = attributes[NSFontAttributeName] as! UIFont
        let attributedString = NSAttributedString(string: text as String, attributes: attributes)

        let textSize = text.sizeWithAttributes(attributes)

        let textPath    = CGPathCreateWithRect(CGRect(x: -x, y: y + font.descender, width: ceil(textSize.width), height: ceil(textSize.height)), nil)
        let frameSetter = CTFramesetterCreateWithAttributedString(attributedString)
        let frame       = CTFramesetterCreateFrame(frameSetter, CFRange(location: 0, length: attributedString.length), textPath, nil)

        CTFrameDraw(frame, context)

        return textSize
    }

    override func drawRect(rect: CGRect) {
        let ctx: CGContext? = UIGraphicsGetCurrentContext()
        var nums: Int = 0
        for i in 0..<60 {
            CGContextSaveGState(ctx)
            CGContextTranslateCTM(ctx, rect.width / 2, rect.height / 2)
            CGContextRotateCTM(ctx, CGFloat(6.0 * Double(i) * M_PI / 180))
            CGContextMoveToPoint(ctx, 72, 0)
            if (i % 5 == 0) {
                nums++
                drawText(ctx!, text: "\(nums)", attributes: [NSForegroundColorAttributeName : UIColor.blackColor().CGColor, NSFontAttributeName : UIFont.systemFontOfSize(12)], x: 42, y: 0)
            }
            CGContextRestoreGState(ctx)
        }

    }
}
let myView = ClockFace(frame: CGRectMake(0, 0, 150, 150))
myView.backgroundColor = .lightGrayColor()
XCPShowView("", view: myView)

因此我的问题是,如何使用Swift旋转Core Text?

2 个答案:

答案 0 :(得分:1)

您可以通过CGAffineTransform实施CGContextSetTextMatrix()来轮播文字。例如:

...
let frame = CTFramesetterCreateFrame(frameSetter, CFRange(location: 0, length: attributedString.length), textPath, nil)
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(CGFloat(M_PI)))
CTFrameDraw(frame, context)

答案 1 :(得分:0)

这15行花了我8个多小时,然后用大量的StackOverflow搜索(很多答案没有/不再有效!)来找出答案。

当心使用CTLineDraw的解决方案。对于CTLineDraw,您必须更改cgContext.textMatrix来进行旋转,但结果却很奇怪/不可靠。生成框架然后旋转该框架证明更加可靠。

使用Swift 5.2 ...

func drawCenteredString(
    cgContext: CGContext,
    string: String,
    targetCenter: CGPoint,
    angleClockwise: Angle,
    uiFont: UIFont
) {
    let attrString = NSAttributedString(string: string, attributes: [NSAttributedString.Key.font: uiFont])
    let textSize = attrString.size()
    let textPath = CGPath(rect: CGRect(x: CGFloat(-textSize.width/2), y: -uiFont.ascender / 2, width: ceil(textSize.width), height: ceil(textSize.height)), transform: nil)
    let frameSetter = CTFramesetterCreateWithAttributedString(attrString)
    let frame = CTFramesetterCreateFrame(frameSetter, CFRange(location: 0, length: attrString.length), textPath, nil)
    cgContext.saveGState()
    cgContext.translateBy(x: targetCenter.x, y: targetCenter.y)
    cgContext.scaleBy(x: 1, y: -1)
    cgContext.rotate(by: CGFloat(-angleClockwise.radians))
    CTFrameDraw(frame, cgContext)
    cgContext.restoreGState()
}