Ios 如何在CGContext中旋转NSString drawinrect
如何将字符串旋转到一定程度?当我把一个字符串和一个图像画在一起时 我提到了这个问题,但我的椭圆消失了Ios 如何在CGContext中旋转NSString drawinrect,ios,objective-c,nsstring,cgaffinetransform,Ios,Objective C,Nsstring,Cgaffinetransform,如何将字符串旋转到一定程度?当我把一个字符串和一个图像画在一起时 我提到了这个问题,但我的椭圆消失了 //Add text to UIImage -(UIImage *)addMoodFound:(int)moodFoundCount andMoodColor:(CGColorRef)mColour { float scaleFactor = [[UIScreen mainScreen] scale]; UIGraphicsBeginImageContextWithOptions
//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;
}
cGraffineTransformMakeRotation
将围绕上下文的原点旋转(在本例中,x=0,y=0)
要正确旋转文本,首先需要将上下文的原点与包含字符串的框的中心平移,然后旋转原点并将其移回原始位置
将应用旋转的3条线替换为:
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(18, 18));
CGContextConcatCTM(context, CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(65)));
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-18, -18));
cGraffineTransformMakeRotation
将围绕上下文的原点旋转(在本例中,x=0,y=0)
要正确旋转文本,首先需要将上下文的原点与包含字符串的框的中心平移,然后旋转原点并将其移回原始位置
将应用旋转的3条线替换为:
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(18, 18));
CGContextConcatCTM(context, CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(65)));
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-18, -18));
请不要在自定义变量前面加上
kCG
。它们不是核心图形的一部分。我在代码中看不到任何转换上下文(即旋转上下文)的内容。你确定那部分能用吗?@DavidRönnqvist我加上了我用来旋转绳子的线。添加3行后,我的文本将消失请不要在自定义变量前面加kCG
。它们不是核心图形的一部分。我在代码中看不到任何转换上下文(即旋转上下文)的内容。你确定那部分能用吗?@DavidRönnqvist我加上了我用来旋转绳子的线。我的文字将消失,一旦我添加了3行的答案。这三条线很有魅力!谢谢你的回答。这三条线很有魅力!