【发布时间】:2010-12-14 19:10:39
【问题描述】:
我想在视图中绘制一些文本,旋转 90°。我对 iPhone 开发还很陌生,在网上翻了一下发现了许多不同的解决方案。我已经尝试了一些,但通常最终我的文本被剪掉了。
这里发生了什么?我正在在一个相当小的空间(表格视图单元格)中绘图,但必须有一种“正确”的方式来做到这一点……对吧?
编辑:这里有几个例子。我正在尝试在左侧的黑条上显示文本“12345”。
-
第一次尝试,来自RJShearman on the Apple Discussions
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSelectFont (context, "Helvetica-Bold", 16.0, kCGEncodingMacRoman); CGContextSetTextDrawingMode (context, kCGTextFill); CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0); CGContextSetTextMatrix (context, CGAffineTransformRotate(CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f ), M_PI/2)); CGContextShowTextAtPoint (context, 21.0, 55.0, [_cell.number cStringUsingEncoding:NSUTF8StringEncoding], [_cell.number length]); CGContextRestoreGState(context);
(来源:deeptechinc.com) -
第二次尝试,来自zgombosi on iPhone Dev SDK。相同的结果(这里的字体略小,因此剪辑较少)。
CGContextRef context = UIGraphicsGetCurrentContext(); CGPoint point = CGPointMake(6.0, 50.0); CGContextSaveGState(context); CGContextTranslateCTM(context, point.x, point.y); CGAffineTransform textTransform = CGAffineTransformMakeRotation(-1.57); CGContextConcatCTM(context, textTransform); CGContextTranslateCTM(context, -point.x, -point.y); [[UIColor redColor] set]; [_cell.number drawAtPoint:point withFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]]; CGContextRestoreGState(context);
【问题讨论】:
-
确实有几种方法可以解决这个问题。提供一些关于你到底尝试了什么以及它到底是如何失败的细节(它是如何/在哪里被剪掉的?)将使回答更容易有用。
标签: iphone text drawing rotation