• iOS 文字转化成图片


    //文字转化成图片

    -(UIImage *)imageFromText:(NSArray*)arrContent withFont:(CGFloat)fontSize withTextColor:(UIColor *)textColor withBgImage:(UIImage *)bgImage withBgColor:(UIColor *)bgColor

    {

        // set the font type and size

        

        UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];

        NSMutableArray *arrHeight = [[NSMutableArray alloc] initWithCapacity:arrContent.count];

        

        CGFloat fHeight = 0.0f;

        for (NSString *sContent in arrContent) {

            CGSize stringSize = [sContent sizeWithFont:font constrainedToSize:CGSizeMake(CONTENT_MAX_WIDTH, 10000) lineBreakMode:NSLineBreakByWordWrapping];

            [arrHeight addObject:[NSNumber numberWithFloat:stringSize.height]];

            

            fHeight += stringSize.height;

        }

        

        CGSize newSize = CGSizeMake(CONTENT_MAX_WIDTH+20, fHeight+50);

        

        // Create a stretchable image for the top of the background and draw it

        UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        

        //如果设置了背景图片

        if(bgImage)

        {

            UIImage* stretchedTopImage = [bgImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];

            [stretchedTopImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

        }else

        {

            if(bgColor)

            {

                //填充背景颜色

                [bgColor set];

                UIRectFill(CGRectMake(0, 0, newSize.width, newSize.height));

            }

        }

        

        CGContextSetCharacterSpacing(ctx, 10);

        CGContextSetTextDrawingMode (ctx, kCGTextFillClip);

        [textColor set];

        

        int nIndex = 0;

        CGFloat fPosY = 20.0f;

        for (NSString *sContent in arrContent) {

            NSNumber *numHeight = [arrHeight objectAtIndex:nIndex];

            CGRect rect = CGRectMake(10, fPosY, CONTENT_MAX_WIDTH , [numHeight floatValue]);

            

            [sContent drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

            

            fPosY += [numHeight floatValue];

            nIndex++;

        }

        

        // transfer image

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        

        return image;

        

    }

  • 相关阅读:
    面向对象property属性、静态方法和类方法
    【枚举】枚举简单使用
    【Mac】Mac快捷键与Mac下的Idea快捷键
    @PostConstruct注解
    【通过ssh oracle11g安装】centos6静默安装oracle11g
    【Zookeeper02】ZK的作用以及使用
    【linux杂记】Ubuntu查看端口使用情况
    【数据结构--二叉树】Java递归实现二叉树遍历
    【python3两小时根本不够】入门笔记04:线程+Lock安全同步
    【python3两小时快速入门】入门笔记03:简单爬虫+多线程爬虫
  • 原文地址:https://www.cnblogs.com/ios988/p/5630369.html
Copyright © 2020-2023  润新知