2011-06-14 61 views
1

我想要像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不显示任何文本(只有一个光标)和不旋转。有没有其他的东西要添加,使其工作?

回答

0

为什么不让视图层次支持?然后,您可以对视图的图层应用仿射变换。

yourView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1.0); 
0

的问题是,您要创建NSAffineTransform,但你不能将其连接到您的文本视图。

+0

对不起,刚添加了缺少的[super drawRect:dirtyRect]。那是你的想法吗?仍然不起作用。 – Nicholas 2011-06-15 09:24:04

+0

尝试:'[self setWantsLayer:YES]; [self layer] .transform = CATransform3DMakeRotation(M_PI_2,1.0,1.0);'? – 2011-06-15 15:17:36

+0

我试过了,但它没有改变任何东西。 – Nicholas 2011-06-15 15:56:18

1

你让它太复杂了。只需设置在视图本身旋转:

[myView setFrameRotation:angleInDegrees]; 

还有setFrameCenterRotation,但应该只针对层支持视图的工作(这是确定)。

有一个警告,我仍然在调查自己:旋转视图时闪烁的文本光标变得非常肥。