开发者

How can I create a string from just the first line of my UITextView?

开发者 https://www.devze.com 2023-03-06 16:22 出处:网络
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\'v

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?


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

[title sizeWithFont:noteTextView.font].width < noteTextView.frame.size.width

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.


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?


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

NSString *textViewString = notesTextView.text;
NSString *titleString = [textViewString substringToIndex:20]

Just use the titleString as the title.

0

精彩评论

暂无评论...
验证码 换一张
取 消