I want to draw a NSString to UIView with every character is vertical,like this:
normal:
and after rotate:
i already have a solution:
- (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);
}
}
My solution is not perfectly draw characters like 'r' or 'l',it makes me crazy!