与核心文本垂直对齐?
如何更改 CTFramesetter 框架中文本的垂直对齐方式?我希望我的文本位于中间,而不是位于顶部。我正在使用核心文本框架。段落的设置可以更改水平对齐方式,但不能更改垂直对齐方式。
How do I change the vertical alignment of the text in a CTFramesetter frame? I want my text to be in the middle instead of being at the top. I am using Core Text framework. There is a setting of the paragraph to change horizontal aligment but not vertical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
终于想通了...
Finally figured it out ...
谢谢尼克,这是一个很棒的片段。
对此进行扩展,如果您使用枚举进行顶部、中间和底部对齐,例如您可以这样做:
Thanks Nick, that was a great snippet.
Just expanding on that, if your doing Top, Middle and Bottom alignment with an enum, for example you could do it like so:
您可以使用 [NSStringboundingRectWithSize:options:attributes:context:] 获取字符串边界框的矩形,它也允许多行文本。
在绘制文本方法中,执行以下操作(RECT 是要在其中绘制文本的矩形):
You can use [NSString boundingRectWithSize:options:attributes:context:] to get the rectangle of your string's bounding box, which allows multi-line text as well.
In your draw text method, do the following (RECT is the rectangle where you want to draw the text):
这说明了一个框架中可以使用多种字体类型和样式的事实(计算文本的高度和宽度,查看
if(index == lastLineIndex)
块以查看高度在哪里计算):参考:Scott Thompson (http://lists.apple .com/archives/quartz-dev/2008/Mar/msg00079.html)
This accounts for the fact that multiple font types and styles can be used in a frame (calculates both height and width of text, look in the
if(index == lastLineIndex)
block to see where the height is calculated):Reference: Scott Thompson (http://lists.apple.com/archives/quartz-dev/2008/Mar/msg00079.html)