如何将NSString绘制到UIView,每个字符都是垂直的

时间:2017-09-16 03:44:33

标签: uilabel drawrect core-text

我想向UIView绘制一个NSString,每个字符都是垂直的,如下所示:

正常:

normal

并在旋转之后:

after rotate 我已经有了解决方案:

- (void)drawTextInRect:(CGRect)rect {
for (int i = 0; i < self.text.length; i++) {
    NSString *subText = [self.text substringWithRange:NSMakeRange(i, 1)];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);


    // Create text
    CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);

    // Rotate the context 90 degrees (convert to radians)
    CGFloat rotate = (self.textDirection == MMLabelTextDirectionLeft)?(-M_PI_2):0;
    CGAffineTransform transform1 = CGAffineTransformMakeRotation(rotate);
    CGContextConcatCTM(context, transform1);

    // Move the context back into the view

    CGSize characterSize = [subText sizeWithAttributes:@{NSFontAttributeName:self.font}];

    CGContextTranslateCTM(context, -20, i*characterSize.height);

    // Draw the string
    [subText drawInRect:rect withAttributes:@{NSFontAttributeName:self.font}];
//        [subText drawAtPoint:CGPointMake(0, i*MAX(characterSize.width, characterSize.height)) withAttributes:@{NSFontAttributeName:self.font}];
    // Clean up

    CGContextRestoreGState(context);
}

}

我的解决方案不是像'r'或'l'那样完美地绘制字符,它让我发疯了!

0 个答案:

没有答案