在 Xcode 上的图像上添加文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8138396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:26:12  来源:igfitidea点击:

Adding text over an image on Xcode

iphonexcodeuilabel

提问by hafedh

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).

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

  • How can I write this text?
  • How to save the background image+the text on one image file ?
  • 我怎样才能写出这段文字?
  • 如何将背景图像+文本保存在一个图像文件中?

Thanks.

谢谢。

回答by Rayfleck

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

回答by Rob

Thanks Rayfleck! It worked.

谢谢雷弗莱克!有效。

To add optional compatibility with retina displays (fixes choppy letters during '@2x' version of image scale up):

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

replace:

代替:

UIGraphicsBeginImageContext(img.size);

with conditional:

有条件的:

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

回答by Rich Carlton

Update for ios7...

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

回答by Jim75

This approach takes in account the screen's scale and it's super intuitive

这种方法考虑了屏幕的比例,非常直观

    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