带图文的图像上的DrawText调整图像的大小

时间:2013-10-25 09:04:49

标签: ios uiimageview quartz-2d drawtext

我正在图像上绘制文字。文本被绘制没有任何问题,但我的文本是动态的,有时它更多的字符或有时它更少。

从下面的图片中,您可以看到“这是超过320幅图像的长信息,其中的图片未来。

我希望所有消息都显示在下一行,因为我的消息是动态的,我想以320 * 480大小的图像显示它,这里我只显示小图像用于测试目的。

这是绘制文字的代码:

-(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

2 个答案:

答案 0 :(得分:5)

尝试此代码:

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

它会帮助。

答案 1 :(得分:2)

使用UIImageView显示图像并添加UILabel作为UIImageView的子视图。

1.首先配置您的UILabel:

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

2.使用内容长度

确认标签框架
- (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.更改标签的框架和setText

-------旧版---------

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