iPhone:绘制旋转文本?
我想在旋转 90° 的视图中绘制一些文本。我对 iPhone 开发还很陌生,在网上浏览后发现了许多不同的解决方案。我尝试过一些,但通常都会以文本被剪切而告终。
这是怎么回事?我在一个相当小的空间(表格视图单元格)中绘图,但是必须有一种“正确”的方法来做到这一点......对吧?
编辑:这里有几个例子。我试图沿着左侧的黑条显示文本“12345”。
第一次尝试,来自 RJShearman 关于 Apple 讨论
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSelectFont(上下文,“Helvetica-Bold”,16.0,kCGEncodingMacRoman); CGContextSetTextDrawingMode(上下文,kCGTextFill); CGContextSetRGBFillColor(上下文, 1.0, 0.0, 0.0, 1.0); CGContextSetTextMatrix (上下文, CGAffineTransformRotate(CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f ), M_PI/2)); CGContextShowTextAtPoint(上下文, 21.0, 55.0, [_cell.number cStringUsingEncoding:NSUTF8StringEncoding], [_cell.number length]); CGContextRestoreGState(上下文);
(来源:deeptechinc.com)第二次尝试,来自 zgombosi在 iPhone 开发 SDK 上。相同的结果(此处的字体稍小,因此剪裁较少)。
CGContextRef context = UIGraphicsGetCurrentContext(); CGPoint 点 = CGPointMake(6.0, 50.0); CGContextSaveGState(上下文); CGContextTranslateCTM(上下文, point.x, point.y); CGAffineTransform textTransform = CGAffineTransformMakeRotation(-1.57); CGContextConcatCTM(上下文,textTransform); CGContextTranslateCTM(上下文,-point.x,-point.y); [[UIColor redColor]设置]; [_cell.number drawAtPoint:point withFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]]; CGContextRestoreGState(上下文);
尝试二。有几乎相同的剪辑 http://dev.deeptechinc.com/sidney/share/iphonerotation/attempt2.png
I want to draw some text in a view, rotated 90°. I'm pretty new to iPhone development, and poking around the web reveals a number of different solutions. I've tried a few and usually end up with my text getting clipped.
What's going on here? I am drawing in a fairly small space (a table view cell), but there has to be a "right" way to do this… right?
Edit: Here are a couple of examples. I'm trying to display the text "12345" along the black bar at the left.
First attempt, from 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);
(source: deeptechinc.com)Second attempt, from zgombosi on iPhone Dev SDK. Identical results (the font was slightly smaller here, so there's less clipping).
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
事实证明,无论行高如何,我的表格单元格总是初始化为 44 像素高,因此我的所有绘图都被从单元格顶部剪切了 44 像素。
要绘制更大的单元格,需要使用
或
设置内容视图的
autoresizingMask
,并且以正确的大小调用drawRect
。在某种程度上,这是有道理的,因为UITableViewCell
的initWithStyle:reuseIdentifier:
没有提及单元格的大小,只有表视图实际上知道每个单元格有多大row 将根据其自身的大小及其委托对tableView:heightForRowAtIndexPath:
的响应。我阅读了 Quartz 2D 编程指南,直到绘制模型和函数开始有意义,绘制旋转文本的代码变得简单明了:
感谢您的提示,看起来我已经准备好了。
It turns out that the my table cell was always initialized 44px high regardless of the row height, so all of my drawing was getting clipped 44px from the top of the cell.
To draw larger cells it was necessary to set the content view's
autoresizingMask
withor
…and
drawRect
is called with the correct size. In a way, this makes sense, becauseUITableViewCell
'sinitWithStyle:reuseIdentifier:
makes no mention of the size of the cell, and only the table view actually knows how big each row is going to be, based on its own size and its delegate's response totableView:heightForRowAtIndexPath:
.I read the Quartz 2D Programming Guide until the drawing model and functions started to make sense, and the code to draw my rotated text became simple and obvious:
Thanks for the tips, it looks like I'm all set.
使用 :-
其中 label 是 UILabel 的对象。
Use :-
where label is the object of UILabel.
这是一个提示。我猜你是在drawRect 中做这个绘图的。为什么不在drawRect周围画一个框架来看看矩形有多大,以及是否这就是你被剪裁的原因。
另一种方法是将文本放入 UILabel 中,然后在将单元格放入 cellForRowAtIndexPath 中时将其旋转 90 度。
Here's a tip. I presume you're doing this drawing in drawRect. Why don't you draw a frame around drawRect to see how big the rect is and if that is why you get clipping.
An alternative is to put your text in a UILabel, and then rotate that 90 degrees when you make your cells in cellForRowAtIndexPath.
您了解 UITableViewDelegate 方法 heightForRowAtIndexPath 对吧?
这是有关各种图形级别方法的简单教程。假设您知道有多大您的文字是您应该能够适当地调整表视图行大小。
另外,我会检查以确保任何转换后的边界实际上满足您的期望。 (使用调试器或日志语句来验证这一点)。
You know about the UITableViewDelegate method heightForRowAtIndexPath right?
Here's a simple tutorial on various graphics level methods. Presuming you know how big your text is you should be able to size your table view row size appropriately.
Also, I'd check to make sure that the bounds after any transform actually meet your expectations. (Either use a debugger or log statement to verify this).
对于 @Sidncious 所说的以及我通过堆栈溢出收集到的内容,我想给出一个使用示例 - 附加我的代码以完全在屏幕左侧绘制标尺,并旋转数字:
to what @Sidnicious said, and what i collected through out stack overflow, i want to give a usage example - appended my code to completely draw a ruler to the left screen side, with numbers rotated:
当我发现我需要将以下内容添加到文件顶部后,我喜欢马特的方法。很简单。
马哈布兹的建议可能是您阻力最小的途径。您可以使用以下命令将 UILabel 旋转 90 度: [label setTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.0f))];您只需根据标签宽度计算单元格高度即可。 -马特 – 马特·朗 11 月 10 日 0:09
After I discovered that I needed to add the following to the top of my file I liked Matt's approach. Very simple.
mahboudz's suggestion will probably be your path of least resistance. You can rotate the UILabel 90deg with this: [label setTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.0f))]; You'll just have to calculate your cell height based upon the label width. -Matt – Matt Long Nov 10 at 0:09