在Xcode上的图像上添加文本 [英] Adding text over an image on Xcode

查看:109
本文介绍了在Xcode上的图像上添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make an iphone application similar to some greeting cards application, where I could write text over some pre prepared background images(cards).


  • 如何撰写此文本?

  • 如何在一个图像文件中保存背景图像+文本?

谢谢。

推荐答案

这是一种将字符串刻录到图像中的方法。您可以根据自己的喜好调整字体大小和其他参数。

Here is a method that burns a string into an image. You can tweak the font size and other parameters to configure it to your liking.

/* 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;
}

这篇关于在Xcode上的图像上添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆