UITextView转UIImage

//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;


}

发布了2 篇原创文章 · 获赞 0 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章