[objective-c]在 UIImage 上添加文本

发布时间: 2016/10/4 3:05:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

嗨我使用此函数在图像上绘制文本后做图像上的绘图,但当我做这其显示我透明文本即有混浊,我不想它应该是与 α 1.0 的时候我尝试添加具有 RGBA 但力的 fontcolor 属性, 工作

-(UIImage*) drawText:(NSString*) text
             inImage:(UIImage*)  image
             atPoint:(CGPoint)   point
             atSize :(CGSize) size
{
    //[NSLog(@"Drawing Text on the Image : Text Posn x:%f  y:%f ",point.x,point.y);
    UIGraphicsBeginImageContext(_mainViewForDrawing.frame.size);

    [image drawInRect:CGRectMake(_mainViewForDrawing.frame
                                 .origin.x,_mainViewForDrawing.frame.origin.y,_mainViewForDrawing.frame.size.width,_mainViewForDrawing.frame.size.height)];
    CGPoint pt;
    pt.x = point.x + _camera2EditText.frame.size.width/2;
    pt.y = point.y + _camera2EditText.frame.size.height/2;

    UITextPosition *post = [_camera2EditText beginningOfDocument];


    CGRect r = [_camera2EditText caretRectForPosition:post];

    //[NSLog(@"Here is the rect: %f %f ",r.origin.x,r.origin.y);

    CGRect rect = CGRectMake(r.origin.x, point.y + r.origin.y, _mainViewForDrawing.frame.size.width,_mainViewForDrawing.frame.size.height);
    [[UIColor whiteColor] set];

    //UIFont *font = [UIFont boldSystemFontOfSize:TextFontSize];

    UIFont *font = [UIFont fontWithName:TEXT_FONT_NAME size:TEXT_FONT_SIZE];

    if([text respondsToSelector:@selector(drawInRect:withAttributes:)])
    {
        //iOS 7
       // NSDictionary *att = @{NSFontAttributeName:font};
        NSDictionary *att = @{ NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor whiteColor]};
        [text drawInRect:rect withAttributes:att];
    }
    else
    {
        //legacy support
        [text drawInRect:CGRectIntegral(rect) withFont:font];
    }

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

    return newImage;
}

如何去做

解决方法 1:

这是我的方法我使用用于绘制文本到图像,希望这将帮助。它使用相反归因的字符串来设置你想要的属性︰

+(UIImage*)drawFront:(UIImage*)image text:(NSString*)text atPoint:(CGPoint)point
{
    UIFont *font = [UIFont fontWithName:@"Halter" size:21];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, (point.y - 5), image.size.width, image.size.height);
    [[UIColor whiteColor] set];

    NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = NSMakeRange(0, [attString length]);

    [attString addAttribute:NSFontAttributeName value:font range:range];
    [attString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];

    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor darkGrayColor];
    shadow.shadowOffset = CGSizeMake(1.0f, 1.5f);
    [attString addAttribute:NSShadowAttributeName value:shadow range:range];

    [attString drawInRect:CGRectIntegral(rect)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

应该是容易让你适应您的需求......让我知道是否你奋斗 !

官方微信
官方QQ群
31647020