【问题标题】:iPhone: Create image from NSString with word wrapiPhone:使用自动换行从 NSString 创建图像
【发布时间】:2012-02-19 09:53:07
【问题描述】:

我正在使用以下函数将 NSString 转换为图像。

-(UIImage *)imageFromText:(NSString *)text FontName:(UIFont *)font
{
    // set the font type and size
    //UIFont *font = [UIFont systemFontOfSize:20.0];  
    CGSize size  = [text sizeWithFont:font];

    UIGraphicsBeginImageContext(size);

    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

    // transfer image
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return image;
}

效果很好。问题是当字符串包含长文本时,它会创建一个宽度太大的图像。如果文本超出范围,我想应用自动换行功能。

那么如何创建带有 NSString 自动换行的图像?

【问题讨论】:

    标签: iphone uiimage nsstring word-wrap


    【解决方案1】:

    你需要在 NSString 上调用drawInRect:withAttributes: 方法。

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多