ios - 在 UIImage 上添加文本

标签 ios objective-c uiimage uigraphicscontext

您好,我正在使用此功能在图像上绘制完成后在图像上绘制文本,但是当我这样做时,它会显示透明文本,即具有不透明度的文本,我不希望它应该使用 alpha 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;
}

如何着手

最佳答案

这是我用来在图像上绘制文本的方法,希望对您有所帮助。它使用属性字符串来设置您想要的属性:

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

应该很容易让您适应您的需求...如果您遇到困难,请告诉我!

关于ios - 在 UIImage 上添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25302799/

相关文章:

iOs 为 VOIP 应用程序播放音频

php - 将字符串限制为 256 个字节

iphone - 在不移除 TabBar 的情况下在 TabBar View 中显示新的 ViewController

ios - 如何通过拖放创建新的 UIWindowScene?

objective-c - NSData 指针与引用

ios - 如何为动画 UIImageView 着色?

ios - 即使应用程序在后台运行,如何使用通知信息更新 TableView?

iphone - 将富文本内容显示为 UIWebView 或 UILabel+CoreText

ios - UIImage/CGImage 改变我的像素颜色

ios - 避免 UIImageView 在不同设备上拉伸(stretch)