Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何将以下有关核心图形的代码转换为Swift?_Objective C_Swift - Fatal编程技术网

Objective c 如何将以下有关核心图形的代码转换为Swift?

Objective c 如何将以下有关核心图形的代码转换为Swift?,objective-c,swift,Objective C,Swift,这是我发现的使pdf中的url可点击的代码 我仍然掌握着objective-c的窍门,非常感谢你的帮助。提前非常感谢 这是我现在翻译的,我很确定有几个错误: - (void) drawTextLink:(NSString *) text inFrame:(CGRect) frameRect { CGContextRef context = UIGraphicsGetCurrentContext(); CGAffineTransform ctm = CGContextGetCTM(

这是我发现的使pdf中的url可点击的代码

我仍然掌握着objective-c的窍门,非常感谢你的帮助。提前非常感谢

这是我现在翻译的,我很确定有几个错误:

- (void) drawTextLink:(NSString *) text inFrame:(CGRect) frameRect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform ctm = CGContextGetCTM(context);

    // Translate the origin to the bottom left.
    // Notice that 842 is the size of the PDF page. 
    CGAffineTransformTranslate(ctm, 0.0, 842);

    // Flip the handedness of the coordinate system back to right handed.
    CGAffineTransformScale(ctm, 1.0, -1.0);

    // Convert the update rectangle to the new coordiante system.
    CGRect xformRect = CGRectApplyAffineTransform(frameRect, ctm);

    NSURL *url = [NSURL URLWithString:text];        
    UIGraphicsSetPDFContextURLForRect( url, xformRect );

    CGContextSaveGState(context);
    NSDictionary *attributesDict;
    NSMutableAttributedString *attString;

    NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
    attributesDict = @{NSUnderlineStyleAttributeName : underline, 
    NSForegroundColorAttributeName : [UIColor blueColor]};
    attString = [[NSMutableAttributedString alloc] initWithString:url.absoluteString attributes:attributesDict];

    [attString drawInRect:frameRect];

    CGContextRestoreGState(context);
}

Swift 4.0中的上述方法应该是这样的

    func drawTextLink(text: NSString, frameRect: CGRect) {
let context: CGContext = UIGraphicsGetCurrentContext()!
    var c = context.ctm

    let cc = CGAffineTransform.translatedBy(c)
    cc.(0.0, 842)

}

您是否尝试转换代码?如果是,您遇到了什么问题?第二行对于melet上下文来说似乎是最难的:CGContext=UIGraphicsGetCurrentContext();context.ctm=如果您编辑的问题包含的细节与这些内容一样多,则最好是,评论更难阅读。同意。。。。。。。。。
func drawTextLink(text: String, frame: CGRect) {
    guard let context = UIGraphicsGetCurrentContext() else {return}
    let ctm = context.ctm
    ctm.translatedBy(x: 0.0, y: 842)
    ctm.scaledBy(x: 1.0, y: -1.0)
    let newFrame = frame.applying(ctm)
    guard let url = URL(string: text) else {return}
    UIGraphicsSetPDFContextURLForRect(url, newFrame)
    context.saveGState()
    let dict = [NSAttributedStringKey.underlineStyle: 1, .foregroundColor: UIColor.red] as [NSAttributedStringKey : Any]
    let attrString = NSAttributedString(string: text, attributes: dict)
    attrString.draw(in: frame)
    context.restoreGState()
}