How do I draw an NSString at an angle?

2020-01-31 04:20发布

Is there a set of string attributes I can specify that will draw the text at an angle when I call:

[label drawAtPoint:textStart withAttributes:attributes];

2条回答
不美不萌又怎样
2楼-- · 2020-01-31 04:51

NSString itself doesn't have rotation, but you can rotate the context. The string will always be drawn "horizontally" as far as the coordinate space goes, but what actual direction that corresponds to depends on the context. Just use NSAffineTransform to spin it as needed.

查看更多
萌系小妹纸
3楼-- · 2020-01-31 04:55

Here's an example that uses a transform to rotate the drawing context. Essentially it's just like setting a color or shadow, just make sure to use -concat instead of -set.

CGFloat rotateDeg = 4.0f;
NSAffineTransform *rotate = [[NSAffineTransform alloc] init];

[rotate rotateByDegrees:rotateDeg];
[rotate concat];

// Lock focus if needed and draw strings, images here.

[rotate release];
查看更多
登录 后发表回答