Iphone 如何移动CGMutablePathRef?
下面有一段代码,它使用Iphone 如何移动CGMutablePathRef?,iphone,ios,cocoa-touch,core-graphics,core-text,Iphone,Ios,Cocoa Touch,Core Graphics,Core Text,下面有一段代码,它使用CoreText将字符串绘制到UIView: - (void) drawRect:(CGRect)rect { CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.text); CGMutablePathRef mutablePathRef = CGPathCreateMutable(); CGPathAd
CoreText
将字符串绘制到UIView
:
- (void) drawRect:(CGRect)rect
{
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.text);
CGMutablePathRef mutablePathRef = CGPathCreateMutable();
CGPathAddRect(mutablePathRef, NULL, self.bounds);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), mutablePathRef, NULL);
CGFloat frameHeight = [self calculateHeightForFrame: frameRef];
//what do I do here to resposition the mutablePathRef?
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameDraw(frameRef, context);
CFRelease(frameRef);
CGPathRelease(mutablePathRef);
CFRelease(framesetter);
}
我初始化帧和帧设置器,并将它们附加到CGMutablePath
。然后,我使用框架的CTLine
原点来计算文本的高度,以便将其垂直居中
在这之后,我有点不知所措。是否有办法将CGMutablePath
的位置移动到新原点,使其垂直居中?我尝试了CGPathMoveToPoint
,但没有成功
或者我需要创建一个全新的CGMutablePath
和一个全新的框架和框架设置器吗?如果是,如何删除CGMutablePath
?如果我释放它,它会被移除吗
任何关于我可以解决此问题的有效方法的提示都非常好,谢谢。一旦您的
CGMutablePathRef
被用于创建帧,修改路径对帧没有影响。此外,CGPathMoveToPoint
不会移动路径本身,而是当前点,允许附加新的子路径
可以通过在绘图前对上下文应用翻译来更改文本的位置
CGContextTranslateCTM(x, y);
显示如何使文本与核心文本垂直居中
基本上,在调用CGPathAddRect()之前,需要确定文本的高度并修改路径 最后,我必须创建一个frameRef来确定高度,然后我必须重新创建frameRef并根据我计算的高度将其重新定位在中心