2016-02-15 55 views
0

我正在嘗試旋轉核心文本,使它儘管它是通過旋轉的上下文生成它是可讀的。核心文本旋轉不正確旋轉 - 斯威夫特

我的問題是,不管旋轉方法如何,生成的文本不是預期的。

這裏是工作的遊樂場:

import Foundation 
import UIKit 
import XCPlayground 


class ClockFace: UIView { 

    func drawText(context: CGContextRef, text: NSString, attributes: [String: AnyObject], x: CGFloat, y: CGFloat, align: 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) 
     CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(align * 43.0)) 
     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)) 
      if (i % 5 == 0) { 
       nums++ 
       drawText(ctx!, text: "\(nums)", attributes: [NSForegroundColorAttributeName : UIColor.blackColor().CGColor, NSFontAttributeName : UIFont.systemFontOfSize(12)], x: 42, y: 0, align: CGFloat(6.0 * Double(i) * M_PI/180)) 
      } 
      CGContextRestoreGState(ctx) 
     } 

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

不管CGAffineTransfomMakeRotation價值的文字難以辨認。

這是爲什麼?我是否正確旋轉文字?

問候,布蘭登

回答

1

我畫根據你的代碼,但修改後的點點鐘面。

基本上我所做的是翻轉和移動for循環外的整個鐘面。您可以看到我將CGContextTranslateCTM()CGContextScaleCTM()drawText()移至drawRect()。由於上述修改,其他一些計算修改很小。

至於CGContextSetTextMatrix(),這裏發生的旋轉是影響每個字符,但不是整個字符串。你可以評論第19行,並取消註釋第21行,得到不同的結果,看看我的意思。

此外,如果您想直接獲取操場文件,可以從here下載。

import Foundation 
import UIKit 
import XCPlayground 


class ClockFace: UIView { 

    func drawText(context: CGContextRef, text: String, attributes: [String: AnyObject], x: CGFloat, y: CGFloat, align: CGFloat) -> CGSize { 
     let font = attributes[NSFontAttributeName] as! UIFont 
     let attributedString = NSAttributedString(string: text, 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) 

     var transform = CGAffineTransformMakeTranslation(x + 10, y + font.descender) 
     transform = CGAffineTransformRotate(transform, align) 
     //CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(align)) 
     CGContextConcatCTM(context, transform); 
     CTFrameDraw(frame, context) 

     return textSize 
    } 

    override func drawRect(rect: CGRect) { 
     let ctx: CGContext? = UIGraphicsGetCurrentContext() 
     CGContextTranslateCTM(ctx, -45, rect.size.height + 5) 
     CGContextScaleCTM(ctx, 1, -1) 
     var nums: Int = 2 
     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)) 
      if (i % 5 == 0) { 
       nums++ 
       nums = nums > 12 ? nums - 12 : nums 
       drawText(ctx!, text: "\(nums)", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor(), NSFontAttributeName : UIFont.systemFontOfSize(12)], x: 42, y: 0, align: CGFloat(6.0 * Double(i) * M_PI/180)) 

      } 
      CGContextRestoreGState(ctx) 
     } 
    } 
} 

let view = ClockFace(frame: CGRectMake(0, 0, 150, 150)) 
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 
XCPlaygroundPage.currentPage.liveView = view 
+0

它的工作原理,謝謝。你從哪裏得到CGContextTranslateCTM(ctx,-45,rect.size.height + 5)的值? –

+0

@BrandonBradley實際上這裏沒有魔法,它只是對''drawText(:text:attributes:x:y:align:)''的幾何x,y位置的補償。目的是試圖讓鐘面位於中心。 – Allen