简体   繁体   English

如何在iPhone中的图像上添加文本?

[英]How can I add a text on an image in iphone?

I need to put a text over an image in iPhone. 我需要在iPhone的图片上加上文字。 It's like the Eurosport iPhone app. 就像Eurosport iPhone应用程序一样。


(source: mzstatic.com ) (来源: mzstatic.com

In the same way I need to put a text in my app. 以同样的方式,我需要在应用程序中添加文本。 How can I do this? 我怎样才能做到这一点?

Thanks. 谢谢。

I found two ways 我发现了两种方法

1: 1:

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 25, 200, 200);
    imageView.image = [UIImage imageNamed:@"Choice set.png"];

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    myLabel.text = @"Hello There";
    myLabel.textColor = [UIColor whiteColor];
    myLabel.backgroundColor = [UIColor blueColor];
    [imageView addSubview:myLabel];

    [self.view addSubview:imageView];

    [imageView release];
    [myLabel release];

2: 2:

Add text to UIImage 向UIImage添加文本

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{

   int w = img.size.width;
   int h = img.size.height;
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

   char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
   CGContextSelectFont(context, "Arial",20, kCGEncodingMacRoman);
   CGContextSetTextDrawingMode(context, kCGTextFill);
   CGContextSetRGBFillColor(context, 0, 0, 0, 1);
   CGContextShowTextAtPoint(context,10,10,text, strlen(text));
   CGImageRef imgCombined = CGBitmapContextCreateImage(context);

   CGContextRelease(context);
   CGColorSpaceRelease(colorSpace);

   UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
   CGImageRelease(imgCombined);

   return retImage;
} 

then call 然后打电话

UIImage *image = [UIImage imageNamed:@"myimage.png"];

imageView.image = [self addText:image text:@"Hello there"];

只是简单地使标签具有清晰的背景。

yourLabel.backgroundColor = [UIColor clearColor];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM