使用UIGraphicsBeginImageContextWithOptions进行绘制文本

// 联系上下文

UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO,0);

NSMutableParagraphStyle*style = [NSMutableParagraphStyledefaultParagraphStyle].mutableCopy;

style.lineBreakMode=NSLineBreakByCharWrapping;

style.alignment=NSTextAlignmentLeft;

style.firstLineHeadIndent=30;

NSString*text =@"但是在iPhone的视网膜屏幕上,如你使用UIGraphicsBeginImageContext这个方法来获取图形上下文进行绘制的话就会出现你绘制出来的图片相当的模糊";

NSMutableAttributedString*attributeString = [[NSMutableAttributedStringalloc]initWithString:textattributes:@{

NSFontAttributeName:[UIFontsystemFontOfSize:15.0],NSForegroundColorAttributeName:[UIColorredColor],NSParagraphStyleAttributeName:style,

}];

[attributeStringdrawInRect:CGRectMake(20,100,self.view.frame.size.width-40, attributeString.size.height+60)];

// 根据上下文生成图片

UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// 将生成的图片添加到contents

self.view.layer.contents= (id)image.CGImage;

你可能感兴趣的:(使用UIGraphicsBeginImageContextWithOptions进行绘制文本)