【问题标题】:DrawText on Image with text Adjusting to the size of the ImageDrawText on Image with text 调整到图像的大小
【发布时间】:2013-10-25 09:04:48
【问题描述】:

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

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

【问题讨论】:

  • 必须绘制文本吗?如果没有,使用 UILabel 是一种更简单的方法
  • 不,实际上我希望文本显示在图像上,我的消息是动态的,我应该将该消息放在图像上。
  • 使用标签可以吗?
  • 是的,更简单,稍后添加答案
  • 看看NSString + UIKit Additions你会找到帮助你的方法。

标签: ios uiimageview quartz-2d drawtext


【解决方案1】:

试试这个代码:

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

会有帮助的。

【讨论】:

  • 这个条件有什么用? ([UIScreen instancesRespondToSelector:@selector(scale)])
  • 如果刻度通过了,这里的刻度=0.0f。检查它-> UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
  • @krish UIScreen.scale 是在 iOS4 中引入的。 if 测试系统是否可以自动选择比例分辨率,如果为真,我们只需将比例设置为 0。对于旧系统,UIGraphicsBeginImageContext() 将比例设置为默认值 = 1.0。虽然,我想简单地使用 UIGraphicsBeginImageContextWithOptions(imgview.frame.size, NO, 0.0f); 就足够了,因为它应该在默认情况下设置 scale = 1.0。
【解决方案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.更改标签的框架和设置文本

-------旧版本---------

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

【讨论】:

  • 顺便说一下,这些方法在iOS7.0及更高版本中可用 -boundingRectWithSize:options:attributes:context:
  • 如果您的应用需要覆盖早期版本,请告诉我,以便我提供旧版本代码
  • 我的应用程序也需要在旧版本上运行。请提供给我
  • 我不想要子视图,我需要创建应该像标签一样显示的图像,背景应该是图像。喜欢我的新问题第一张图片
  • 子视图只能达到你的效果,而不是低级
猜你喜欢
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2020-09-16
相关资源
最近更新 更多