¿Cómo puedo agregar un texto en una imagen en iphone?

I need to put a text over an image in iPhone. It's like the Eurosport iPhone app.


(fuente: mzstatic.com)

In the same way I need to put a text in my app. How can I do this?

Gracias.

preguntado el 31 de julio de 12 a las 12:07

simple, just put an UILabel al UIImageView and you've done it! -

2 Respuestas

I found two ways

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:

Add text to 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;
} 

luego llame

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

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

respondido 31 mar '14, 11:03

it is probably too complicated for this simple issue. - hoyox

@Ramshad +1 on the second method. Very useful. Everything got released properly, too. Thanks. - impío

Just simple make label with clear background.

yourLabel.backgroundColor = [UIColor clearColor];

Respondido 31 Jul 12, 12:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.