- (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);
}
我找到的代码可以点击pdf来制作网址。
我仍然掌握了目标-c,所以你的帮助非常适合。非常感谢你提前!
现在这就是我所翻译的内容,而且我很确定会有一些错误:
func drawTextLink(text: NSString, frameRect: CGRect) {
let context: CGContext = UIGraphicsGetCurrentContext()!
var c = context.ctm
let cc = CGAffineTransform.translatedBy(c)
cc.(0.0, 842)
}
答案 0 :(得分:0)
Swift 4.0中的上述方法应该是这样的。
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()
}