Question

I am drawing the text on the image. text is drawn without any problem , but my text is dynamic sometimes its more characters or sometimes its less.

From below image you can see "This is long message more than 320 width of image , in that of image doesn't come .

I want all the Message to be displayed in the next line, because my Message is dynamic and i want to display it in 320 *480 size of image, here i am displaying small image just for testing purpose .

This is the Code for drawing Text :

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

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

    char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];

    CGContextSelectFont (context, // 3
                         "Helvetica-Bold",
                         15,
                         kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1); 
    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}

enter image description here

Was it helpful?

Solution 2

Use a UIImageView to display your image and add a UILabel as the subview of UIImageView.

1.configure your UILabel first:

[yourLabel setNumberOfLines:0];
[yourLabel setLineBreakMode:NSLineBreakByWordWrapping];

2.get your label frame with the contents length

- (CGRect)labelFrameWithText:(NSString *)text
{
    CGRect rect;

    // the font of your text
    UIFont *font = [UIFont systemFontOfSize:15.0]; 
    NSDictionary *attributes = @{NSFontAttributeName: font};

    // the first parametric CGSize is the max size that the rect's size can be
    CGRect boundingRect = [title boundingRectWithSize:CGSizeMake(youImageWidth, 100.0)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];

    //the rect of the UILabel
    //This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
    rect = CGRectMake(yourLabelOriginX,
                      yourLabelOriginY,
                      ceil(boundingRect.size.width),
                      ceil(boundingRect.size.height));

    return rect;
}

3.change your label's frame and setText

------- old version ---------

CGSize contentSize = [content sizeWithFont:font
                             constrainedToSize:CGSizeMake(maxWidth, maxHeight)
                                 lineBreakMode: NSLineBreakByWordWrapping];

OTHER TIPS

Try This code:

lblText.text=[NSString stringWithFormat:@"Put Your Text"];
lblText.font = [UIFont fontWithName:@"Helvetica" size:10.0f];
CGPoint point=lblText.center;
imgview.image =[self drawText:lblText.text inImage:imgview.image atPoint:point];


    //Save it in Document directory for Checking Purpose...
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *image1 =   imgview.image; // imageView is my image from camera
    NSData *imageData = UIImagePNGRepresentation(image1);
    [imageData writeToFile:savedImagePath atomically:NO];


 -(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point
  {
     UIFont *font =lblText.font;;    
     if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
          UIGraphicsBeginImageContextWithOptions(imgview.frame.size, NO, 0.0f);
      } else {
        UIGraphicsBeginImageContext(imgview.frame.size);
      }

       [image drawInRect:CGRectMake(0,0,imgview.frame.size.width,imgview.frame.size.height)];
       CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);//Set Frame as per your Requirement 
       [lblText.textColor set];
       [text drawInRect:CGRectIntegral(rect) withFont:font];
       UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

     return newImage;
 }

It will Help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top