Iphone 在Xcode上的图像上添加文本

Iphone 在Xcode上的图像上添加文本,iphone,xcode,uilabel,Iphone,Xcode,Uilabel,我想制作一个类似于一些贺卡应用程序的iphone应用程序,我可以在一些预先准备好的背景图像(卡片)上写文本 我怎样写这篇课文 如何将背景图像+文本保存在一个图像文件中 谢谢。这里有一个将字符串烧录成图像的方法。您可以调整字体大小和其他参数以根据自己的喜好进行配置 /* Creates an image with a home-grown graphics context, burns the supplied string into it. */ - (UIImage *)burnTextI

我想制作一个类似于一些贺卡应用程序的iphone应用程序,我可以在一些预先准备好的背景图像(卡片)上写文本

  • 我怎样写这篇课文
  • 如何将背景图像+文本保存在一个图像文件中

谢谢。

这里有一个将字符串烧录成图像的方法。您可以调整字体大小和其他参数以根据自己的喜好进行配置

/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {

UIGraphicsBeginImageContext(img.size);

CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];

[[UIColor redColor] set];           // set text color
NSInteger fontSize = 14;
if ( [text length] > 200 ) {
    fontSize = 10;
}
UIFont *font = [UIFont boldSystemFontOfSize: fontSize];     // set text font

[ text drawInRect : aRectangle                      // render the text
         withFont : font
    lineBreakMode : UILineBreakModeTailTruncation  // clip overflow from end of last line
        alignment : UITextAlignmentCenter ];

UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();   // extract the image
UIGraphicsEndImageContext();     // clean  up the context.
return theImage;
}

谢谢你,雷克!成功了

要添加与视网膜显示器的可选兼容性(在图像放大的“@2x”版本中修复了断断续续的字母):

替换:

UIGraphicsBeginImageContext(img.size);
有条件:

if (UIGraphicsBeginImageContextWithOptions != NULL)
    UIGraphicsBeginImageContextWithOptions(img.size,NO,0.0);
else
    UIGraphicsBeginImageContext(img.size);
ios7的更新

 /* Creates an image with a home-grown graphics context, burns the supplied string into it. */
    - (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {

        UIGraphicsBeginImageContext(img.size);

        CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
        [img drawInRect:aRectangle];

        [[UIColor redColor] set];           // set text color
        NSInteger fontSize = 14;
        if ( [text length] > 200 ) {
            fontSize = 10;
        }

        UIFont *font = [UIFont fontWithName:@"Courier" size:fontSize];
        NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;


        paragraphStyle.alignment = NSTextAlignmentRight;
        NSDictionary *attributes = @{ NSFontAttributeName: font,
                                      NSParagraphStyleAttributeName: paragraphStyle,
                                      NSForegroundColorAttributeName: [UIColor whiteColor]};
        [text drawInRect:aRectangle withAttributes:attributes];

        UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();   // extract the image
        UIGraphicsEndImageContext();     // clean  up the context.
        return theImage;
    }

这种方法考虑了屏幕的大小,而且非常直观

    UIImage * img = ...
    UIImageView * iV = [[UIImageView alloc] initWithImage:img];
    UILabel * l = [[UILabel alloc] initWithFrame:iV.bounds];
    l.textAlignment = ...;
    l.adjustsFontSizeToFitWidth = YES;
    l.textColor = ...;
    l.font = ...;
    l.text = ...;

    [iV addSubview:l];

    UIGraphicsBeginImageContextWithOptions(iV.bounds.size, NO, 0);
    [iV.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return finalImage

核心图形可能是正确的方向。您可以制作一个隐藏状态栏的空白控制器,并在完成编辑后制作一个屏幕截图。我认为UIImagePNGRE演示文稿正是您所需要的。谢谢您的回答。是的,我认为你的解决方案比使用核心图形要容易得多,我将重点介绍这一点。如果我理解,你的意思是我在uiview上准备背景图像,并添加一个带有透明背景的uiTextfield,让用户输入文本,然后进行屏幕截图。可能是吗?谢谢,雷克,看起来很棒@Rayfleck我知道这是一个古老的答案,但我今天才找到它,它非常有效,所以谢谢你!我只有一个问题。有没有办法修改此代码以垂直绘制字符串?(文本逆时针旋转90度)@Ryan-首先顺时针旋转图像,然后烧录文本,然后再将其旋转回来。在这里寻找一些方便的UIImage扩展来进行旋转。@Rayfleck真聪明。谢谢它只是返回一个空图像?有什么想法吗?很乐意帮忙!IF语句是一个快速检查,以查看UIGraphicsBeginImageContextWithOptions是否可用(ios4或更高版本)。如果不是,那么你可以假设它不是视网膜。如果是,则传递选项:1-图像大小(文本区域);2-透明背景(不透明=否);3-“0.0”,告诉设备使用自己的刻度(视网膜显示默认为2.0(?)