2015-09-30 41 views
0

我得到两个错误,因为我尝试我的应用程序升级到斯威夫特2如何让kCGPathStroke在Swift 2中工作?

两个错误是:

Use of unresolved identifier 'kCGPathStroke'

和:

Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String : AnyObject]?'

两个问题在评论代码如下。我查看了文档,看起来像kCGPathStroke仍然存在,所以我很困惑,为什么这是打破。我究竟做错了什么?

import UIKit 
import CoreGraphics 
class MIBadgeLabel: UILabel { 
    override func drawRect(rect: CGRect) 
    { 
     // Drawing code 
     let ctx: CGContextRef = UIGraphicsGetCurrentContext()! 
     let borderPath: UIBezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners:UIRectCorner.AllCorners, cornerRadii: CGSizeMake(10.0, 10.0)) 

     CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor) 
     CGContextSaveGState(ctx) 
     CGContextAddPath(ctx, borderPath.CGPath) 
     CGContextSetLineWidth(ctx, 4.0) 
     CGContextSetStrokeColorWithColor(ctx, UIColor.clearColor().CGColor) 
     CGContextDrawPath(ctx, kCGPathStroke) //Swift 2 error 
     CGContextRestoreGState(ctx) 

     CGContextSaveGState(ctx) 
//  CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor) 
     var textFrame: CGRect = rect 
     let labelString: NSString = self.text! as NSString 
     let textSize: CGSize = labelString.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(13.0)]) 
     textFrame.size.height = textSize.height 

     textFrame.origin.y = rect.origin.y + ceil((rect.size.height - textFrame.size.height)/2.0) 

     let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle(); 
     paragraphStyle.alignment = .Center 

     var attributes: NSMutableDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.whiteColor(), NSParagraphStyleAttributeName:paragraphStyle] 

     labelString.drawInRect(textFrame, withAttributes: attributes) // Swift 2 error 
    } 
} 
+0

为什么下来投票? –

回答

3

第一个错误:

CGContextDrawPath(ctx, kCGPathStroke) 

像这样:

CGContextDrawPath(ctx, .Stroke) 

二错误:

var attributes: NSMutableDictionary = [ 
    NSFontAttributeName: UIFont.systemFontOfSize(13.0), 
    NSForegroundColorAttributeName : UIColor.whiteColor(), 
    NSParagraphStyleAttributeName:paragraphStyle 
] 

像这样:

let attributes = [ 
    NSFontAttributeName: UIFont.systemFontOfSize(13.0), 
    NSForegroundColorAttributeName : UIColor.whiteColor(), 
    NSParagraphStyleAttributeName:paragraphStyle 
] 
+0

非常感谢! –

0

为了解决这个标识符 'kCGPathStroke' 的问题,使用雨燕2.0,使用:

CGContextDrawPath(ctx, CGPathDrawingMode.Stroke) 
相关问题