【问题标题】:Adding Text To UIImage...Wrong spot将文本添加到 UIImage ......错误的地方
【发布时间】:2017-01-24 19:39:27
【问题描述】:

我正在尝试将文本绘制到 UIImage。下面的这种方法有效,但文本已关闭。我在框架的中心有*l 标签。在self.image = [self drawText]之后,文字在图片的左上角。

-(UIImage*) drawText{
    UIGraphicsBeginImageContextWithOptions(_creatorImageView.image.size, false, _creatorImageView.image.scale);
    [_creatorImageView.image drawInRect:CGRectMake(0,0,_creatorImageView.image.size.width, _creatorImageView.image.size.height)];

    NSLog(@"Caption ARRY: %@", _creatorImageView.captionArray);
    for (UILabel *l in _creatorImageView.captionArray){
        NSLog(@"%f   %f", l.frame.origin.x, l.frame.origin.y);
        CGRect rect = CGRectMake(l.frame.origin.x, l.frame.origin.y, _creatorImageView.image.size.width, _creatorImageView.image.size.height);
        [l.textColor set];
        CGFloat radians = atan2f(l.transform.b, l.transform.a);
        CGFloat degrees = radians * (180 / M_PI);

       //CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeScale(l.transform.tx, l.transform.ty));
       //CGContextConcatCTM(UIGraphicsGetCurrentContext(),    CGAffineTransformMakeRotation(degrees));

        //NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        //[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
        //[paragraphStyle setAlignment:NSTextAlignmentCenter];

        NSDictionary *attributes = @{ NSFontAttributeName: l.font,
                                  NSForegroundColorAttributeName: l.textColor,
                                  NSBackgroundColorAttributeName: l.backgroundColor};
                                  //NSParagraphStyleAttributeName:paragraphStyle};

        [l.text drawInRect:rect withAttributes:attributes];
        NSLog(@"TEXT: %@", l.text);

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

    return newImage;
}

【问题讨论】:

    标签: ios objective-c uiimage uigraphicscontext


    【解决方案1】:

    你正在使用代码

    [l.text drawInRect:rect withAttributes:attributes];
    

    绘制文本。您指定的 rect 矩形控制文本的绘制位置,在您的上下文坐标中(其中 0,0 是上下文的左上角。)

    如果您希望您的文本在您的上下文中居中,请在您的上下文中计算中心,并调整矩形的原点以使文本居中。

    我会让你计算数学。只需几行代码。

    【讨论】:

    • rect.x = 119.5rect.y = 340.5。这是标签坐标。这就是为什么我很困惑为什么它在左上角。 @邓肯C
    猜你喜欢
    • 2016-07-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多