我想要像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