1

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

从下图中您可以看到“这是超过 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];
}

在此处输入图像描述

4

2 回答 2

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

我会帮你的。

于 2013-10-25T09:58:24.117 回答
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];
于 2013-10-25T10:02:07.853 回答