iPhone:使用自动换行从NSString创建图像

问题描述 投票:0回答:1

我正在使用以下函数将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个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.