核心文本与 UITextView 文本格式完全相同?
我正在尝试设置一个与 iPad 中的 UITextView
文本格式完全匹配的 CTFrame
。 首先,我将 UITextView 的文本转换为属性字符串。然后我设置了绘图框的宽度和高度,Core Text 将在其中绘制文本。 我成功使用 Core Text 绘制文本,但即使我使用相同的字体和大小,UITextView
和 Core Text 显示的结果也略有不同。 具体来说,当我使用 [UIFont systemFontOfSize:21]
时,UITextView
中的每个空格都比 Core Text 的结果多 1 个像素。
对于短句或单词来说还可以,但是如果 UITextView
和 Core Text 有多行,它们的结果就会变得非常不同。例如,UITextView
在行尾对一个单词执行自动换行,而 Core Text 将该单词保留在同一行中。如果您看到附图,最后一个单词“paragraph”的起始位置已经非常不同(由于 8 个空格字符而导致 8 个像素间隙)。
更糟糕的是,如果我使用不同的字体(例如添加到项目中的自定义字体),UITextView
中的每个字符都会多 1 个像素。
我使用 Core Text 来查找 UITextView 中当前光标的像素位置,因此它们两者应该完全匹配,每行包含相同数量的字符和单词。
问题:有没有办法让Core Text对象完美匹配UITextView的文本格式?
谢谢!
这是我如何设置属性字符串的代码。 (我刚刚遵循了核心文本指南。)
CTFontRef font = CTFontCreateWithName((CFStringRef) [UIFont systemFontOfSize:21.0].fontName, 21.0, NULL);
CFMutableAttributedStringRef attrString2 = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString2, CFRangeMake(0, 0), (CFStringRef) string);
CFAttributedStringSetAttribute(attrString2, CFRangeMake(0, [string length]),kCTFontAttributeName, font);
-
-
这是一张图片。
I'm trying to set up a CTFrame
that exactly matches my UITextView
's text format in iPad.
First of all, I converted UITextView
's text to an attributed string. Then I set up a width and a height of drawing box in which Core Text will draw text.
I succeeded to draw text using Core Text, but UITextView
and Core Text show slightly different results even though I used the same font and size.
Specifically, when I used [UIFont systemFontOfSize:21]
, each space in UITextView
has one more pixel than Core Text's result.
It's okay for a short sentence or word, but if UITextView
and Core Text have multiple lines, their result become very different. For example, UITextView
performs word-wrapping for one word at the end of line, while Core Text keeps that word in the same line. If you see the attached picture, the start positions of the last word "paragraph" are already very different (8 pixel gap due to 8 space characters).
More badly, if I use different fonts such as a custom font added to my project, each character in UITextView
has 1 pixel more.
I'm using Core Text to find the pixel-position of the current cursor in UITextView
, so both of them should perfectly match each other, containing the same number of characters and words in each line.
Question: Is there a way to make Core Text object that perfectly matches UITextView's text format?
Thank you!
Here's a code how I set up attributed string. (I just followed Core Text Guide.)
CTFontRef font = CTFontCreateWithName((CFStringRef) [UIFont systemFontOfSize:21.0].fontName, 21.0, NULL);
CFMutableAttributedStringRef attrString2 = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString2, CFRangeMake(0, 0), (CFStringRef) string);
CFAttributedStringSetAttribute(attrString2, CFRangeMake(0, [string length]),kCTFontAttributeName, font);
-
-
Here's a picture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的解决方案可能适用于特定磅值的特定字体,但一般情况下您不能依赖它。
CoreText 与普通的 UILabels、UITextView:s 或 UIStringDrawing 根本不兼容,因此您不能混合使用它们。
您要么必须仅使用 CT 函数来处理所有字符串(包括实现自定义输入,如果您需要的话),要么根本不使用它们。
Your solution might work for a specific font at a specific point size, but you can't rely on it in general.
CoreText is simply not compatible with normal UILabels, UITextView:s or UIStringDrawing, so you can't mix them.
Either you have to use only CT functions for all string handling (including implementing custom input if that is what you need) or not use them at all.
回答我自己。
我刚刚找到了非常简单的解决方案!使用任何字体编辑器,您只需更改空格字符的宽度(ascii 值 32); UITextView 的原始字体和 Core Text 的修改字体,反之亦然。我使用免费软件字体编辑器 FontForge。虽然我仍然需要做一些极端情况的测试,例如在同一行中写入日语字符和英语字母等,但现在几乎可以在 UITextView 中找到光标/插入符号的像素位置。
Answer to myself.
I just found very simple solution! Using any font editor, you can just change the width of space character (ascii value 32); an original font for UITextView and a modified font for Core Text or vice versa. I used a freeware font editor FontForge. Though I still have to do some extreme-case tests such as writing Japanese characters and English alphabets in the same line and so on, now it becomes almost possible to find a pixel-position of a cursor/caret in UITextView.