如何将以下关于CGContext的代码转换为Swift3

时间:2017-03-26 06:58:10

标签: ios swift3 cgcontext

就像标题一样,我有一些Objective-c代码,如何在Swift3中使用它们

CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, size.width, size.height));
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:_text];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, _text.length)];

CTFramesetterRef ctFramesetting = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attri);
CTFrameRef ctFrame = CTFramesetterCreateFrame(ctFramesetting, CFRangeMake(0, attri.length), path, NULL);
CTFrameDraw(ctFrame, context);

CFRelease(path);
CFRelease(ctFramesetting);
CFRelease(ctFrame);

2 个答案:

答案 0 :(得分:3)

这是一个干净的Swift 3版本:

context.saveGState()
context.textMatrix = CGAffineTransform.identity
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)

let path = CGMutablePath()
let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
path.addRect(rect, transform: .identity)

let attrString = NSMutableAttributedString(string: _text as String)
attrString.addAttribute(NSFontAttributeName,
                        value: UIFont.systemFont(ofSize: 10.0),
                        range: NSRange(location: 0, 
                                       length: _text.length))

let ctFramesetting = CTFramesetterCreateWithAttributedString(attrString)
let ctFrame = CTFramesetterCreateFrame(ctFramesetting, 
                                       CFRangeMake(0, attrString.length), 
                                       path, 
                                       nil)
CTFrameDraw(ctFrame, context)

我建议你不要使用转换器

<强>为什么吗 使用转换器,您将

  • 可能使用需要常量的变量
  • 桥梁/施法值
  • 明确展开选项
  • 打破Swift风格(这取决于你实际使用的风格,但仍然如此)
  • 打破语言惯例

这意味着您将获得需要重构的不稳定/脏代码

答案 1 :(得分:-1)

您可以使用类似Objective-C to Swift Converter的内容来获取以下代码:

context.saveGState()
context.textMatrix = CGAffineTransform.identity
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
var path: CGMutablePathRef = CGMutablePath()
path.addRect(CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.width), height: CGFloat(size.height)), transform: .identity)
var attri = NSMutableAttributedString(string: _text)
attri.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: CGFloat(10)), range: NSRange(location: 0, length: _text.length))
var ctFramesetting: CTFramesetterRef? = CTFramesetterCreateWithAttributedString((attri as? CFAttributedStringRef))
var ctFrame: CTFrameRef = CTFramesetterCreateFrame(ctFramesetting, CFRangeMake(0, attri.length), path, nil)
CTFrameDraw(ctFrame, context)

但我建议您确实学习Objective-C和Swift的基本原则,这样您就可以自己进行这种转换,而不是依赖其他人或外部工具:)

例如,以上是从转换器中输出的代码。但是,如果你要做一些清理,它看起来像这样:

guard let context = UIGraphicsGetCurrentContext() else {
    return
}
context.saveGState()
context.textMatrix = CGAffineTransform.identity
context.translateBy(x: 0, y: size.height)
context.scaleBy(x:1.0, y: -1.0)
let path = CGMutablePath()
path.addRect(CGRect(x:0, y:0, width:size.width, height:size.height), transform:.identity)
let attri = NSMutableAttributedString(string:_text as String)
attri.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize:10), range: NSRange(location: 0, length:_text.length))
let ctFramesetting = CTFramesetterCreateWithAttributedString(attri)
let ctFrame = CTFramesetterCreateFrame(ctFramesetting, CFRangeMake(0, attri.length), path, nil)
CTFrameDraw(ctFrame, context)

正如您所注意到,必须添加以下内容才能获得CGContext个实例:

guard let context = UIGraphicsGetCurrentContext() else {
    return
}

此外,一些标记为可变(或var)的变量已更改为不可变(或let)实例。

但并非如此。上面的代码假定_text的类型为NSString。要使用原生Swift类型,您应该将其更改为使用String而不是NSString。然后代码变为:

guard let context = UIGraphicsGetCurrentContext() else {
    return
}
context.saveGState()
context.textMatrix = CGAffineTransform.identity
context.translateBy(x: 0, y: size.height)
context.scaleBy(x:1.0, y: -1.0)
let path = CGMutablePath()
path.addRect(CGRect(x:0, y:0, width:size.width, height:size.height), transform:.identity)
let attri = NSMutableAttributedString(string:_text)
attri.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize:10), range: NSRange(location: 0, length:_text.characters.count))
let ctFramesetting = CTFramesetterCreateWithAttributedString(attri)
let ctFrame = CTFramesetterCreateFrame(ctFramesetting, CFRangeMake(0, attri.length), path, nil)
CTFrameDraw(ctFrame, context)

因此,为了将代码转换为Swift 3并使其工作,您需要了解Swift和Objective-C之间的差异。