我想要像Adobe Illustrator一样拥有可旋转的文本编辑区域。我试过几件事,包括继承NSTextView并添加这样的绘制代码之前坐标系旋转:如何旋转NSTextView
@implementation MyNSTextView
-(void)drawRect:(NSRect)dirtyRect
{
NSAffineTransform * trafo = [NSAffineTransform transform];
[trafo rotateByDegrees:45];
[trafo concat];
[super drawRect:dirtyRect];
}
-(void)drawInsertionPointInRect:(NSRect)rect color:(NSColor *)color turnedOn:(BOOL)flag
{
NSAffineTransform * trafo = [NSAffineTransform transform];
[trafo rotateByDegrees:45];
[trafo concat];
[super drawInsertionPointInRect:rect color:color turnedOn:flag];
}
导致文本损坏显示器。我还尝试通过继承NSView并以编程方式组装文本体系结构来实现自己的功能。我用我的NSLayoutManager的drawGlyphsForRange:方法绘制字符串。这部分工作,但变得复杂,因为我必须旋转绘图的坐标和处理鼠标点击我必须将鼠标位置转换回旧坐标。
那么这是做到这一点的最佳方式,还是有其他的东西?
编辑:我现在试图用CALayer的是这样的:
@implementation MyNSTextView
-(id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
CALayer *myLayer = [CALayer layer];
myLayer.transform = CATransform3DMakeRotation(1.6,1,0,1.0);
[self setLayer:myLayer];
[self setWantsLayer:YES];
}
return self;
}
但是,这产生了一个TextView不显示任何文本(只有一个光标)和不旋转。有没有其他的东西要添加,使其工作?
对不起,刚添加了缺少的[super drawRect:dirtyRect]。那是你的想法吗?仍然不起作用。 – Nicholas 2011-06-15 09:24:04
尝试:'[self setWantsLayer:YES]; [self layer] .transform = CATransform3DMakeRotation(M_PI_2,1.0,1.0);'? – 2011-06-15 15:17:36
我试过了,但它没有改变任何东西。 – Nicholas 2011-06-15 15:56:18