美文网首页
把文字转换成图片

把文字转换成图片

作者: 化二缺 | 来源:发表于2019-01-10 15:09 被阅读5次
- (UIImage *)imageWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color
{
    CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, 30) lineBreakMode:NSLineBreakByWordWrapping];
    NSDictionary *attributes = @{NSFontAttributeName : font,
                                 NSForegroundColorAttributeName : color};
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];

    UIGraphicsBeginImageContextWithOptions(size, 0, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSetCharacterSpacing(ctx, 10);

    CGContextSetTextDrawingMode(ctx, kCGTextFill);

    CGContextSetRGBFillColor(ctx, 255, 255, 255, 1);

    [attributedString drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

相关文章

网友评论

      本文标题:把文字转换成图片

      本文链接:https://www.haomeiwen.com/subject/ehjarqtx.html