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

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.

Related

UITextView right to left Unicode \u202B not working

I am essentially making a teleprompter app and I need a UITextView to display right to left for EVERY line.
NSString *textString = #"Hello There\nMy name is Mark";
textView.text = [#"\u202B" stringByAppendingString: textString];
This is not working. I need this to read
" erehT olleH"
" kraM si eman yM"
I understand that I also need fonts that are upside down etc.. I need to get this part fixed first. Thanks.
The notation \u202b denotes the Unicode character U+202B is RIGHT-TO-LEFT EMBEDDING, which does not affect the writing direction of characters with strong directionality, such as Latin letters.
The character U+202E RIGHT-TO-LEFT OVERRIDE (\u202e) forces right-to-left writing direction, overriding the inherent directionality of characters. To end its effect, use the U+202C POP DIRECTIONAL FORMATTING character:
'\u202eHello There\nMy name is Mark \u202c'
This has little to do with fonts. The rendering engine is supposed to handle some characters like “(” using mirrored symbols, e.g. so that “(foo)” gets rendered as “(oof)” and not “)oof(” in right-to-left writing. But generally, no mirroring is involved; letters remain the same, they just run right to left.
If you actually want to have text mirrored, you need something completely different (a transformation).
This is the logic for reversing the string... I hope this helps you... Just append this string in your textView.text
NSString *sampleString = #"Hello this is sample \n Hello there";
NSMutableString *reverseString = [NSMutableString string];
NSInteger index = [sampleString length];
while (index > 0)
{
index--;
NSRange subStrRange = NSMakeRange(index, 1);
[reverseString appendString:[sampleString substringWithRange:subStrRange]];
}
NSLog(#"%#", reverseString);

How to use regular expression in iPhone app to separate string by , (comma)

I have to read .csv file which has three columns. While parsing the .csv file, I get the string in this format Christopher Bass,\"Cry the Beloved Country Final Essay\",cbass#cgs.k12.va.us. I want to store the values of three columns in an Array, so I used componentSeparatedByString:#"," method! It is successfully returning me the array with three components:
Christopher Bass
Cry the Beloved Country Final Essay
cbass#cgs.k12.va.us
but when there is already a comma in the column value, like this
Christopher Bass,\"Cry, the Beloved Country Final Essay\",cbass#cgs.k12.va.us
it separates the string in four components because there is a ,(comma) after the Cry:
Christopher Bass
Cry
the Beloved Country Final Essay
cbass#cgs.k12.va.us
so, How can I handle this by using regular expression. I have "RegexKitLite" classes but which regular expression should I use. Please help!
Thanks-
Any regular expression would probably turn out with the same problem, what you need is to sanitize your entries or strings, either by escaping your commas or by highlighting strings this way: "My string". Otherwise you will have the same problem. Good luck.
For your example you would probably need to do something like:
\"Christopher Bass\",\"Cry\, the Beloved Country Final Essay\",\"cbass#cgs.k12.va.us\"
That way you could use a regexp or even the same method from the NSString class.
Not related at all, but the importance of sanitizing strings: http://xkcd.com/327/ hehehe.
How about this:
componentsSeparatedByRegex:#",\\\"|\\\","
This should split your string whereever " and , appear together in either order, resulting in a three-member array. This of course assumes that the second element in the string is always enclosed in parentheses, and the characters " and , never appear consecutively within the three components.
If either of these assumptions is incorrect, other methods to identify string components may be used, but it should be made clear that no generic solution exists. If the three component strings can contain " and , anywhere, not even a limited solution is possible in such cases:
Doe, John,\"\"Why Unescaped Strings Suck\", And Other Development Horror Stories\",Doe, John <john.doe#dev.null>
Hopefully there is nothing like the above in your CSV data. If there is, the data is basically unusable, and you should look into a better CSV exporter.
The regex you're searching for is: \\"(.*)\\"[ ^,]*|([^,]*),
in ObjC: (('\"' && string_1 && '\"' && 0-n spaces) || string_2 except comma) && comma
NSString *str = #"Christopher Bass,\"Cry, the Beloved Country ,Final Essay\",cbass#cgs.k12.va.us,som";
NSString *regEx = #"\\\"(.*)\\\"[ ^,]*|([^,]*),";
NSMutableArray *split = [[str componentsSeparatedByRegex:regEx] mutableCopy];
[split removeObject:#""]; // because it will print always both groups even if the other is empty
NSLog(#"%#", split);
// OUTPUT:
2012-02-07 17:42:18.778 tmpapp[92170:c03] (
"Christopher Bass",
"Cry, the Beloved Country ,Final Essay",
"cbass#cgs.k12.va.us",
som
)
RegexKitLite will add both strings to the array, therefore you will end up with empty objects for your array. removeObject:#"" will delete those but if you need to maintain true empty values (eg. your source has val,,ue) you have to modify the code to the following:
str = [str stringByReplacingOccurrencesOfRegex:regEx withString:#"$1$2∏"];
NSArray *split = [str componentsSeparatedByString:#"∏"];
$1 and $2 are those two strings mentioned above, ∏ is in this case a character which will most likely never appear in normal text (and is easy to remember: option-shift-p).
The last part looks like it will never contain a comma. Neither will the first one as far as I can see...
What about splitting the string like this:
NSArray *splitArr = [str componentsSeparatedByString:#","];
NSString *nameStr = [splitArr objectAtIndex:0];
NSString *emailStr = [splitArr lastObject];
NSString *contentStr = #"";
for(int i=1; i<[splitArr count]-1; ++i) {
contentStr = [contentStr stringByAppendingString:[splitArr objectAtIndex:i]];
}
This will use the first and last string as is, and combine the rest into the content.
Kind of a hack, but a name and an email address will never contain a comma, right?
Is the title guarantied to have the quotation marks? And is it the only component that can have them? Because then componentSeparatedByString:#"\"" should get you this:
Christopher Bass,
Cry, the Beloved Country Final Essay
,cbass#cgs.k12.va.us
Then use componentSeparatedByString:#"," or substringFrom/ToIndex: to get rid of the two commas in the first and last component.
Here's a solution using substring:
NSString* input = #"Christopher Bass,\"Cry, the Beloved Country Final Essay\",cbass#cgs.k12.va.us";
NSArray* split = [input componentsSeparatedByString:#"\""];
NSString* part1 = [split objectAtIndex:0];
NSString* part2 = [split objectAtIndex:1];
NSString* part3 = [split objectAtIndex:2];
part1 = [part1 substringToIndex:[part1 length] - 1];
part3 = [part3 substringFromIndex:1];
NSLog(part1);
NSLog(part2);
NSLog(part3);

NSArray in a Label

I would like to display the contents of the NSMutable array in a label.
I have the following code that displays only the last object. What would be the method to display ALL the objects in the array (in this case "values")?
self.lblMessage.text = [NSString stringWithFormat:#"%#\n%#",
self.lblMessage.text, [values objectAtIndex:[values count]-1]];
Following code should do what you need:
label.numberOfLines = 0; // to make sure your label is able to display multiple lines
label.text = [values componentsJoinedByString:#"\n"]; //insert separator symbol you need in place of "\n"
To get all values in an NSArray joined by a delimiter like ", " use [values componentsJoinedByString:#", "]. The delimiter can of course be "\n" if you like, but you need to make sure your label or textfield supports multiple lines.
Also, your [values objectAtIndex:[values count]-1] can be better expressed as [values lastObject]. :)
Normally a label is only to show one line of text. And you use \n in your code. So there are multiple lines. Delete The \n in your code or try tu use a UITextView. ;-)
There's also a way to force UILabel to display multiple lines, but I don't know that one on the go...

\n not working in UIlabel

I've seen similar questions here, but still can figure out why it's not working.
How to add line break (in other words add new paragraph) in multiline UIlabel?
I have a label with a lot of text,
lbl.numberOfLines = 0;
lbl.sizeToFit;
but I'm still getting something like this:
"Some text here\nAnd here I want new line"
Thank you
This is an old question, but just wanted to let you know that \r works like a charm :)
UILabel won't respect \n, You can use option-return (in Interface builder) to force where you want a line break.
You can use a UIWebView in place of the label and then you can format however you like.
(And set the lineBreakMode as AngeDeLaMort says above.)
You can try this One :
lbl.text = #"My \n label";
lbl.numberofLines = 0;
This should work:
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
Your issue may be different... but mine was a literal character \n, which is actually \\n in memory.
I solved it with:
label.text = [rawText stringByReplacingOccurrencesOfString#"\\n" withString:#"\n"];
You still need to set line break mode as well as numberOfLines for it to work.
actually there is a way to do that. try
unichar chr[1] = {'\n'};
NSString *singleCR = [NSString stringWithCharacters:(const unichar *)chr length:1];
[yourLabel setText:[NSString stringWithFormat:#"new%#line",singleCR]];
If your string is really ok, maybe you can try adding this line as well:
lbl.lineBreakMode = UILineBreakModeWordWrap;
use ctrl + Enter in storyboard, and make number of lines as 0
I know it's old question, but i was wondering - could the problem be lbl.sizeToFit; ?
if you could set frame like CGRectMake(0,0,300,300) - does that solves the problem? Because \n works on my side.
NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet;
NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:#"\n"];
UILabel use \n to made new line. \n need a space in the back.
This is old question but if any one want to do from interface builder.then it is easy and simple
select label and change type to attributed by default it is plain
now wherever you want to \n there just use Alt + Enter and you will get new line
hope it is helpful to someone who is using interface builder to achieve this

How can I detect if a string contains punctuation marks at the end?

Lets assume I have the string:
"Hello I like your shoes #today...!"
I am tokenizing the string by spaces:
return [string componentsSeparatedByString:#" "];
So my array contains:
Hello
I
like
your
shoes
#today...!
I want to focus on "#today...!" any word that has a # in the prefix I am changing the font color.
How can I make sure that only "#today" has its font color changed?
I would basically like to figure out if a word has a punctuation mark at the end, and change the color for characters before the punctuation mark.
I'm confused by your question. Are you trying to detect string with punctuation marks at the end or with # marks at the beginning?
In any case:
for (NSString *word in [string componentsSeparatedByString:#" "]) {
if ([word hasPrefix:#"#"])NSLog(#"%# starts with #",word);
if ([word hasSuffix:#"!"])NSLog(#"%# end with !",word);
}
You could do something like:
if ([[NSCharacterSet symbolCharacterSet] characterIsMember:[word characterAtIndex:0]]) NSLog(#"%#", word);
This is to test for a symbol at the beginning of the string--to test at the end, you would use [word characterAtIndex:([word length] - 1)]
EDIT: OK, I think I understand the question now. If you want want to only change the color of characters before the color set, you could do something along the lines of:
NSRange punctCharRange = [word rangeOfCharacterFromSet:[NSCharacterSet punctuationCharacterSet]];
for (int i = 0; i < punctCharRange.location; i++) {
//change the color of the character
}
Consider using RegexKitLite and this method:
- (NSRange)rangeOfRegex:(NSString *)regex
options:(RKLRegexOptions)options
inRange:(NSRange)range
capture:(NSInteger)capture
error:(NSError **)error;
The regex you'd want is something like #"#\\S+\\b". This regex says "Find a '#' character followed by 1 or more non-space characters followed by a word boundary".
This would then return the range of the matched regex inside the string to match.
The following code should look from the end of word toward the beginning until it finds a character that is not alphanumeric, test if it found such a character and if so, remove anything after that character. (untested code)
NSString *wordWithoutTrailingNonAlphanum;
NSRange firstNonAlphanumCharFromEnd =
[word rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]
options:NSBackwardsSearch];
if (firstNonAlphanumCharFromEnd.location != NSNotFound) {
wordWithoutTrailingNonAlphanum =
[word substringToIndex:(firstNonAlphanumCharFromEnd.location + 1)];
}
I tried tokenizing all words that begin with # and splitting it based on the NSPunctuationCharacterSet. This works, however I lose the punctuation marks.