【问题标题】:Drawing NSString in CGRect在 CGRect 中绘制 NSString
【发布时间】:2012-09-19 02:27:37
【问题描述】:

我正在尝试在 CGRect 的底部绘制一个 NSString。到目前为止,我已经能够将文本置于顶部居中,但我需要将其置于底部。我也尝试过 DrawAtPoint 但没有奏效。这是我目前拥有的代码: UIGraphicsBeginImageContext(img.size);

CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];

[[UIColor whiteColor] set];
NSInteger fontSize = 25;
UIFont *font = [UIFont boldSystemFontOfSize: fontSize];

[text drawInRect:aRectangle withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];



UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【问题讨论】:

    标签: iphone nsstring uiimage cgrect


    【解决方案1】:

    您可以使用以下方法: sizeWithFont:constrainedToSize:lineBreakMode 获取文本大小,然后计算左上点并使用drawAtPoint绘制文本

    【讨论】:

    • 我尝试使用 drawAtPoint 查看时间,但由于某种原因,它没有在矩形顶部添加绘图。
    • 你也可以drawInRect,但是矩形大小是通过sizeWithFont:constrainedToSize:lineBreakMode计算出来的
    • 这是我的全部代码,以便您更好地了解我在做什么。
    • 没关系,我不能在评论中发布那么多字符;我实际上是在调用一个函数,它正在返回这个 Rect 但由于某种原因 NSString.drawAtPoint 没有添加到 Rect 中。
    • drawAtPoint 只画单线,不换行。
    【解决方案2】:

    使用这个方法:

     -(UIImage *)imageFromText:(NSString *)text
    {
      CGSize maximumSize = CGSizeMake(300, 1000); //set width for string to wrap.
      UIFont *font = [UIFont boldSystemFontOfSize:16];
      CGSize strSize = [text sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
      if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(strSize,NO,0.0);
      else
        // iOS is < 4.0 
        UIGraphicsBeginImageContext(strSize);
    
      CGRect newframe = CGRectMake(0, 0, strSize.width, strSize.height);
      [text  drawInRect:newframe 
                      withFont:font 
                 lineBreakMode:UILineBreakModeWordWrap 
                     alignment:UITextAlignmentLeft]; 
      UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();    
      return testImg;
    }
    

    【讨论】:

    • 我尝试使用您的代码,但返回的图像只是黑色的。我尝试添加 [[UIColor whiteColor] set];将文本颜色设置为白色,但这不起作用。
    • 我的错误,图像刚刚返回非常小;我可以在 UIImage 的底部添加该字符串吗?这基本上就是我想要做的所有事情。
    【解决方案3】:

    我发现这段代码对我的问题非常有帮助。

    - (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];// "05/05/09";
        CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(context, kCGTextFill);
        CGContextSetRGBFillColor(context, 255, 255, 255, 1);
    
    
        //rotate text
        CGContextShowTextAtPoint(context, 0, 5, text, strlen(text));
    
    
        CGImageRef imageMasked = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
    
        return [UIImage imageWithCGImage:imageMasked];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多