如何将NSString旋转到一定程度?当我和图像一起画一个字符串时。
我提到了这个问题Drawing rotated text with NSString drawInRect,但我的椭圆已经消失了。
//Add text to UIImage
-(UIImage *)addMoodFound:(int)moodFoundCount andMoodColor:(CGColorRef)mColour
{
float scaleFactor = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO,scaleFactor);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
//CGContextSetRGBFillColor(context, kCGAggressiveColor);
CGContextSetFillColorWithColor(context,mColour);
CGContextFillEllipseInRect(context, CGRectMake(0, 0, 36, 36));
CGContextSetRGBFillColor(context, 250, 250, 250, 1);
//nsstring missing after adding this 3 line
CGAffineTransform transform1 = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(65));
CGContextConcatCTM(context, transform1);
CGContextTranslateCTM(context, 36, 0);
/////////////////////////////////////////////// /
[[NSString stringWithFormat:@"%d",moodFoundCount ] drawInRect : CGRectMake(0, 7, 36, 18)
withFont : [UIFont fontWithName:monR size:17]
lineBreakMode : NSLineBreakByTruncatingTail
alignment : NSTextAlignmentCenter ];
CGContextRestoreGState(context);
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
答案 0 :(得分:5)
CGAffineTransformMakeRotation
将围绕上下文的原点旋转(在这种情况下x = 0,y = 0)。
要正确旋转文本,您需要首先使用包含字符串的框的中心翻译上下文的原点,旋转并将原点移回原位。
将您使用旋转的3行替换为:
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(18, 18));
CGContextConcatCTM(context, CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(65)));
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-18, -18));