如何仅从 UITextView 的第一行创建一个字符串?
我正在制作一个类似于notes.app 的UITextView,其中textView 的第一行用作标题。我需要创建一个仅包含第一行文本的新字符串。到目前为止,我已经想出了这个:
NSRange startRange = NSMakeRange(0, 1);
NSRange titleRange = [noteTextView.text lineRangeForRange:startRange];
NSString *titleString = [noteTextView.text substringToIndex:titleRange.length];
NSLog(@"The title is: %@", titleString);
唯一的问题是它依赖于用户按 Return 键。我还尝试使用循环来查找第一行中的字符数:
CGSize lineSize = [noteTextView.text sizeWithFont:noteTextView.font
constrainedToSize:noteTextView.frame.size
lineBreakMode:UILineBreakModeWordWrap];
int textLength =1;
while ((lineSize.width < noteTextView.frame.size.width) &&
([[noteTextView.text substringToIndex:textLength] length] < [noteTextView.text length]))
{
lineSize = [[noteTextView.text substringToIndex:textLength] sizeWithFont:noteTextView.font
constrainedToSize:noteTextView.frame.size
lineBreakMode:UILineBreakModeWordWrap];
textLength = textLength+1;
}
NSLog(@"Length is %i", textLength);
但我在某个地方遇到了这个错误 - 它返回字符总数,而不是第一行上的数字。
有谁知道更简单/更好的方法吗?
I am making a UITextView which is similar to notes.app, where the first line of the textView is used as the title. I need to create a new string which contains only the first line of text. So far I've come up with this:
NSRange startRange = NSMakeRange(0, 1);
NSRange titleRange = [noteTextView.text lineRangeForRange:startRange];
NSString *titleString = [noteTextView.text substringToIndex:titleRange.length];
NSLog(@"The title is: %@", titleString);
The only problem with this is that it relies on the user pressing Return. I've also tried using a loop to find the number of characters in the first line:
CGSize lineSize = [noteTextView.text sizeWithFont:noteTextView.font
constrainedToSize:noteTextView.frame.size
lineBreakMode:UILineBreakModeWordWrap];
int textLength =1;
while ((lineSize.width < noteTextView.frame.size.width) &&
([[noteTextView.text substringToIndex:textLength] length] < [noteTextView.text length]))
{
lineSize = [[noteTextView.text substringToIndex:textLength] sizeWithFont:noteTextView.font
constrainedToSize:noteTextView.frame.size
lineBreakMode:UILineBreakModeWordWrap];
textLength = textLength+1;
}
NSLog(@"Length is %i", textLength);
But I've got this wrong somewhere - it returns the total number of characters, instead of the number on the first line.
Does anyone know an easier/better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 CoreText 可能有更好的方法,但我将其扔掉只是因为它突然出现在我的脑海中。
您可以将字符逐一添加到 NSMutableString *title 中,然后
删除最后一个字符,显然会一路进行必要的边界检查,并在必要时删除最后添加的字符。
但 sizeWithFont 太慢了。因此,如果您经常这样做,您可能需要考虑“标题”的另一种定义 - 例如,20 个字符后的第一个断词。
但同样,CoreText 可能会产生更多可能性。
There is probably a much better way with CoreText, but I'll throw this out there just because it came to mind off the top of my head.
You could add characters one by one to an NSMutableString *title while
then drop the last one, obviously doing the necessary bounds checking along the way and dropping the last added character if necessary.
But sizeWithFont is sloooooow. So if you're doing this often you might want to consider another definition of 'title' - say, at first word break after 20 chars.
But again, CoreText might yield more possibilities.
我不明白你上面的代码。只找到字符串中文本的第一行,例如直到 CR 或 LF 终止第一行,不是更简单吗?
如果没有 CR 或 LF,那么您将获取整个文本,因为此时只有一行。
当然,这不会给您带来第一行中可见的内容,以防该行较长并被换行,但我认为使用 lineRangeForRange 也不会这样做,或者会吗?
如果您唯一关心的是“用户必须按 Enter 键”才能使其工作,那么为什么不在测试第一行的长度之前简单地在文本中附加一个换行符呢?
I do not understand the code you're having above. Wouldn't it be simpler do just find the first line of text in the string, e.g. until a CR or LF terminates the first line?
And if there is no CR or LF, then you take the entire text as you have only one line then.
Of course, this will give you not what is visible in the first line in case the line is longer and gets wrapped, but I think that using lineRangeForRange doesn't do this, either, or does it?
And if your only concern is that "the user has to press enter" to make it work, then why not simply append a newline char to the text before testing for the first line's length?
查看文本视图的一行可以容纳多少个字符,并在 substringToIndex: 方法中使用该数字。像这样:
重复输入相同的字符并计算一行中有多少个字符。确保使用宽字母以确保可靠性。使用大写的 g 或 m 或 q 或 w 或您正在使用的字体中最宽的任何内容。
假设一行可以容纳 20 个字符。
然后
只需使用
titleString
作为标题。See how many characters can fit in one line of your text view and use that number in a substringToIndex: method. Like this:
Type out the same character repeatedly and count how many fit in one line. Make sure to use a wide letter to ensure reliability. Use a capital g or m or q or w or whatever is widest in the font you're using.
Say 20 characters can fit in one line.
Then do
Just use the
titleString
as the title.