2013-03-27 79 views
0

我正在繪製一個文本框,並且我在UIView中添加了一個文本框。文本在路徑中。它繪製正確,但內容從底部開始並結束於頂部。有沒有辦法來解決這個問題?coretext顯示文本從底部到頂部的文本域

這裏是我的代碼:

// Draw text into a circle using Core Text and Quartz 
- (void) drawRect:(CGRect)rect 
{ 
[super drawRect: rect]; 

CGContextRef context = UIGraphicsGetCurrentContext(); 
self.layer.rasterizationScale = [[UIScreen mainScreen] scale]; 

// Flip the context 
CGContextTranslateCTM(context, 0, 3); 
CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0f, -1.0f)); 


// Stroke that path 
CGContextAddPath(context, backPath); 
CGContextSetLineWidth(context, 1.0f); 
//[[UIColor blackColor] setStroke]; 
CGContextStrokePath(context); 

// Fill that path 
CGContextAddPath(context, backPath); 
[[UIColor whiteColor] setFill]; 
CGContextFillPath(context); 

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); 
CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, string.length),backPath, NULL); 
CTFrameDraw(theFrame, context); 

CFRelease(framesetter); 
CFRelease(theFrame); 
CFRelease(backPath); 

} 

這如何使路徑:

- (id) initWithAttributedString: (NSAttributedString *) aString path: (vector<ofPoint>)paths 
{ 
backPath = CGPathCreateMutable(); 

CGPathMoveToPoint(backPath, NULL, paths[0].x, paths[0].y); // Bottom left 

for(int x=1; x< paths.size(); x++){ 
CGPathAddLineToPoint(backPath, NULL, paths[x].x,paths[x].y); // Bottom right 
} 
CGPathCloseSubpath(backPath); 

if (!(self = [super initWithFrame:CGRectZero])) return self; 

self.backgroundColor = [UIColor clearColor]; 
string = aString; 
return self; 
} 

img

而爲了讓文本字段:

path.push_back(ofPoint(ofGetWidth()/4,ofGetHeight()/4)); 
    // point to user 
// path.push_back(ofPoint((position.x-(ofGetWidth()/4))/2,ofGetHeight()/4)); 
    // point to right top 
    path.push_back(ofPoint(ofGetWidth()/4,ofGetHeight()/4)); 
    // point to end x, pos y user 
    path.push_back(ofPoint((ofGetWidth()/4),(position.y-(ofGetHeight()/4))/2)); 

    path.push_back(ofPoint((position.x-(ofGetWidth()/4))/2-100,(position.y-(ofGetHeight()/4))/2-100)); 

    path.push_back(ofPoint((position.x-(ofGetWidth()/4))/2,(position.y-(ofGetHeight()/4))/2)); 

回答

2

你是flippi文本矩陣。相反,翻轉整個CTM:

CGContextScaleCTM(context, 1, -1); 
CGContextTranslateCTM(context, 0, self.bounds.size.height); 
+0

好的,我應該在哪裏添加這部分? – 2013-03-27 19:42:35

+0

我已經在過去做過。但它只是使textField怪異。要創建上面的文本字段,我使用了上面添加的示例 – 2013-03-27 19:58:33