【问题标题】:How to rotate String and saved this String as UIImage in swift如何快速旋转字符串并将此字符串保存为 UIImage
【发布时间】:2015-06-29 05:45:43
【问题描述】:

我需要旋转String 并将这个旋转字符串保存为UIImage

假设我给定的字符串是 = HELLO WORLD

现在我需要先将它旋转(比如 45 度),然后保存为 UIImage,这样我就可以将这个 UIImage 显示到我的 UIImageView

请帮帮我。所以我可以旋转String 并将这个旋转字符串保存为UIImage in Swift?。

我找到了这个答案Drawing rotated text with NSString drawInRect。但无法达到结果。

【问题讨论】:

    标签: ios swift uiimage cgaffinetransform uigraphicscontext


    【解决方案1】:

    可能是下面的代码对你有帮助,我实现了将字符串旋转 45 度的相同结果

    -(UIImage*) drawText:(NSString*) text
             atPoint:(CGPoint)   point
    {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(65, 65),NO,0.0);
    
    CGRect rect = CGRectMake(point.x, point.y, 50, 30);
    [[UIColor whiteColor] set];
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeTranslation(32.5, 32.5));
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeRotation(M_PI / 4));
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), CGAffineTransformMakeTranslation(-32.5, -32.5));
    //[text drawInRect:CGRectIntegral(rect) withFont:font ];
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:12],
                                  NSForegroundColorAttributeName: [UIColor blackColor],
    
                                  NSParagraphStyleAttributeName:paragraphStyle};
    
    [text drawInRect:CGRectIntegral(rect) withAttributes:attributes];
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
    }
    

    下面是我实现结果的截图

    【讨论】:

    猜你喜欢
    • 2017-03-16
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2015-04-09
    相关资源
    最近更新 更多