在Swift中旋转文本

时间:2016-07-29 01:19:55

标签: ios swift

我试图用swift在自定义视图中绘制一些字符串。我在Android上使用Java做了同样的事情,结果就是:

Android Sample

使用Swift,我得到了这个图形:

enter image description here

这是我的代码:

let π:CGFloat = CGFloat(M_PI)

extension NSString {
    func drawWithBasePoint(basePoint: CGPoint, andAngle angle: CGFloat, andAttributes attributes: [String: AnyObject]) {
        let textSize: CGSize = self.sizeWithAttributes(attributes)
        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        let t: CGAffineTransform = CGAffineTransformMakeTranslation(basePoint.x, basePoint.y)
        let r: CGAffineTransform = CGAffineTransformMakeRotation(angle)
        CGContextConcatCTM(context, t)
        CGContextConcatCTM(context, r)
        self.drawAtPoint(CGPointMake(textSize.width / 2, -textSize.height / 2), withAttributes: attributes)
        CGContextConcatCTM(context, CGAffineTransformInvert(r))
        CGContextConcatCTM(context, CGAffineTransformInvert(t))
    }
}

override func drawRect(rect: CGRect) {
        elements = delegate!.getRotaryElements(self)
        let center = CGPoint(x:bounds.width / 2, y: bounds.height / 2)
        let sectorSize = 2 * π / CGFloat.init(elements.count)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = NSTextAlignment.Center
        for i in 0...elements.count - 1 {
            let attrs = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Thin", size: 14)!, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName:(i % 2 == 0 ? UIColor.redColor():primaryColor)]
            elements[i].drawWithBasePoint(center, andAngle: CGFloat(i) * sectorSize + (sectorSize / 2), andAttributes: attrs)
        }
    }

如何在Swift中生成相同的东西?我是Swift(以及iOS)的新手,并且非常感谢你能给我的任何暗示。

2 个答案:

答案 0 :(得分:2)

尝试下面的代码,看看它是否有效。

let π:CGFloat = CGFloat(M_PI)

extension NSString {
    func drawWithBasePoint(basePoint: CGPoint, andAngle angle: CGFloat, andAttributes attributes: [String: AnyObject]) {
        let radius: CGFloat = 100
        let textSize: CGSize = self.sizeWithAttributes(attributes)
        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        let t: CGAffineTransform = CGAffineTransformMakeTranslation(basePoint.x, basePoint.y)
        let r: CGAffineTransform = CGAffineTransformMakeRotation(angle)
        CGContextConcatCTM(context, t)
        CGContextConcatCTM(context, r)
        self.drawAtPoint(CGPointMake(radius-textSize.width/2, -textSize.height/2), withAttributes: attributes)
        CGContextConcatCTM(context, CGAffineTransformInvert(r))
        CGContextConcatCTM(context, CGAffineTransformInvert(t))
    }
}

答案 1 :(得分:1)

在Swift 5.0中:

    extension String {
    func drawWithBasePoint(basePoint: CGPoint,
                           andAngle angle: CGFloat,
                           andAttributes attributes: [NSAttributedString.Key : Any]) {
        let textSize: CGSize = self.size(withAttributes: attributes)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        let t: CGAffineTransform = CGAffineTransform(translationX: basePoint.x, y: basePoint.y)
        let r: CGAffineTransform = CGAffineTransform(rotationAngle: angle)
        context.concatenate(t)
        context.concatenate(r)
        self.draw(at: CGPoint(x: textSize.width / 2, y: -textSize.height / 2), withAttributes: attributes)
        context.concatenate(r.inverted())
        context.concatenate(t.inverted())
    }
}