zoukankan      html  css  js  c++  java
  • IOS-将长文字转化成图片方法

    我们在看微博时,会看到一些长图片上的显示文章,现在就介绍下如何实现。分析下还是很简单的,总结如下:1、计算文字区域的高 2、利用UIGraphics图形上下文方法来实现 3、验证方法:UIImageWriteToSavedPhotosAlbum,在本地相册中查看成功与否。

    -(UIImage *)imageFromText:(NSArray*) arrContent withFont: (CGFloat)fontSize

    {

        // set the font type and size

        UIFont *font = [UIFontsystemFontOfSize:fontSize];

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

        

        CGFloat fHeight = 0.0f;

        for (NSString *sContent in arrContent) {

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

            [arrHeight addObject:[NSNumbernumberWithFloat:stringSize.height]];

            fHeight += stringSize.height;

        }

        

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

        

        UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGContextSetCharacterSpacing(ctx, 10);

        CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);

        CGContextSetRGBFillColor (ctx, 0.1, 0.2, 0.3, 1); // 6

        CGContextSetRGBStrokeColor (ctx, 0, 0, 0, 1);

        

        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:UILineBreakModeWordWrapalignment:UITextAlignmentLeft];

            

            fPosY += [numHeight floatValue];

            nIndex++;

        }

        // transfer image

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

        return image;

        

    }


  • 相关阅读:
    CSS3自定义滚动条样式 -webkit-scrollbar
    仿flash的文字动画效果
    使用PowerDesigner导出MySQL数据库建模
    将博客搬至CSDN
    centos6.3安装MySQL 5.6(转)
    # mysql -u root -p -bash: mysql: command not found
    win8设置保护眼睛的颜色
    网关末尾要么是1要么是254
    虚机centos和本机Windows之间文件的拷贝无法用xftp时用FileZilla也行
    Java基础知识总结之基础数据类型
  • 原文地址:https://www.cnblogs.com/riasky/p/3507256.html
Copyright © 2011-2022 走看看