为什么我的文字是从下往上绘制的?
在我的自定义视图中,我有以下代码:
-(void) drawRect:(CGRect) rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
![NSString *myString = @"Hello World, This is for\nhttp://www.stackoverflow.com";
// Prepare font
CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 12, NULL);
// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, (CFStringRef)myString, attr);
CFRelease(attr);
// Draw the string
//CTLineRef line = CTLineCreateWithAttributedString(attrString);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.frame);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
//CGContextSetTextMatrix(context, CGAffineTransformIdentity); // this line is wrong, use the below line
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGContextSetTextPosition(context, 20, 12);
CTFrameDraw(frame, context);
// Clean up
CFRelease(path);
CFRelease(frame);
CFRelease(attrString);
CFRelease(font);
}
但是,看起来像这样:
如何使文本向下,并且从左上角开始?
In my custom view, I have the following code:
-(void) drawRect:(CGRect) rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
![NSString *myString = @"Hello World, This is for\nhttp://www.stackoverflow.com";
// Prepare font
CTFontRef font = CTFontCreateWithName(CFSTR("Times"), 12, NULL);
// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, (CFStringRef)myString, attr);
CFRelease(attr);
// Draw the string
//CTLineRef line = CTLineCreateWithAttributedString(attrString);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.frame);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
//CGContextSetTextMatrix(context, CGAffineTransformIdentity); // this line is wrong, use the below line
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGContextSetTextPosition(context, 20, 12);
CTFrameDraw(frame, context);
// Clean up
CFRelease(path);
CFRelease(frame);
CFRelease(attrString);
CFRelease(font);
}
However, that looks like this:
How can I make the text go down, and also start from the top left?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是副本&粘贴我的drawRect:方法之一,它工作得很好。
只需将
__labelData.attributedString
替换为您的属性字符串即可。Here's the copy & paste of one of my drawRect: methods, which does work perfectly.
Just replace
__labelData.attributedString
with your attributed string.