在图像中绘制旋转的文本

时间:2017-02-11 02:54:32

标签: ios objective-c xcode

我的应用程序插入标签并有两个滑块旋转并缩放文本。我想在图像中绘制此文本。请参阅滑块的代码和绘制文本的方法。

- (IBAction)angleSliderDidChange:(id)sender {
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleSlider.value * M_PI / 180.0);
    [myNewLabel setTransform:transform];
}

- (IBAction)scaleSliderDidChange:(id)sender {
    [myNewLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:21*scaleSlider.value]];
    [myNewLabel sizeToFit];
    [myNewLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:17*scaleSlider.value]];
}

- (IBAction)editionDidFinish:(id)sender {
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:17*scaleSlider.value];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentRight;
    NSDictionary *attributes = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraphStyle};

    UIGraphicsBeginImageContext(bigImageView.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGPoint contextCenter = CGPointMake(CGRectGetMidX(myNewLabel.frame), CGRectGetMidY(myNewLabel.frame));
    CGContextTranslateCTM(ctx, contextCenter.x, contextCenter.y);
    CGContextRotateCTM(ctx, angleSlider.value * M_PI / 180);
    CGContextScaleCTM(ctx, scaleSlider.value, scaleSlider.value);
    [myNewLabel.text drawInRect:(CGRect){-myNewLabel.frame.size.width/2, -myNewLabel.frame.size.height/2, myNewLabel.frame.size} withAttributes:attributes];
    finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    bigImageView.image = finalImage;
}

这是一个简单的程序,但我的文字并没有在添加标签的相同位置绘图。我相信我忘记了一些细节,但我无法看到它。结果如下图所示。所以,请帮我解决。

enter image description here

0 个答案:

没有答案