UITextView转UIImage

来源:互联网 发布:网络业务代表招聘 编辑:程序博客网 时间:2024/04/29 02:45
//TextView转image
-(UIImage*) saveTextView
{
    
    //1.设置大小
    CGSize size  = _maskText.size;//
    _rect=_maskText.frame;
    
    
  
    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(size,NO,0);

    
    // draw in context, you can use  drawInRect/drawAtPoint:withFont:
  
    // 2.设置text属性
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
   
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSShadow *shadow=[[NSShadow alloc]init];
    shadow.shadowBlurRadius=2;//模糊度
    shadow.shadowColor=[UIColor blackColor];
    shadow.shadowOffset=CGSizeMake(1, 1);

    //设置文字属性

    //此处具体设置根据个人情况添加,具体设置请看iOS- 详解文本属性Attributes 


    NSDictionary *dict=@{NSFontAttributeName:_maskText.font,NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSShadowAttributeName:shadow,NSVerticalGlyphFormAttributeName:@(0)};
    //NSStrokeColorAttributeName:[UIColor blackColor],NSStrokeWidthAttributeName:@0.5
    
    [_maskText.text drawInRect:CGRectMake(0, 0, size.width, size.height) withAttributes:dict];
    
    
    //3. 转换为图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    _angle=0;
    return image;


}

0 0
原创粉丝点击