\n not working in UIlabel - iphone

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

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 can I create a string from just the first line of my UITextView?

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.

How to capitalized first letter of text

There is some text in a text field. Before setting it to txtfdName, I want to capitalize the first letter.
[addrecipe.txtfdName setText:txtfield1.text];
Use autocapitalizationType for capital letter of UITextField.
txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
Use this code to capitalized first latter
NSString *abc = #"this is test";
abc = [NSString stringWithFormat:#"%#%#",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1] ];
NSLog(#"abc = %#",abc);
To capitalize the first letter of each word manually in code:
NSString* name = [addrecipe.txtfdName setText:txtfield1.text];
name = [name capitalizedString];
Use autocapitalizationType property of UITextInputTraits protocol which is conform by both UITextView and UITextField
txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
txtfield1.autocapitalizationType = UITextAutocapitalizationTypeWords;
or you can also try
[addrecipe.txtfdName setText:[txtfield1.text capitalizedString]]; // the first character from each word in the receiver changed to its corresponding uppercase value
select txtfield1 and go to Attributes Inspector , there is a Capitalization with a drop down menu and select menu form Following
Word : if you select Word than first character of every word is Capital
Sentences : if you Sentence than first character of First word of every sentence is Capital
All Characters : All Characters are Capital
In XCODE 10.0, swift 3, I had to use the following:
textField.autocapitalizationType = UITextAutocapitalizationType.none
textField.autocapitalizationType = UITextAutocapitalizationType.words
textField.autocapitalizationType = UITextAutocapitalizationType.sentences
textField.autocapitalizationType = UITextAutocapitalizationType.allCharacters

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...

how to give "\n" character in label text?

I have coded like that, but can't get output:
label.text = #"hai\nyou";
but the output is hai you. I want: hai and then you must be in the next line.
...
label.text = #"hai\nyou";
label.numberOfLines = 0;
Try to set the "# of Lines" to 0 and let me know if it works.
Or, if you are typing in the little box in IB use option-return to insert a line feed (\n).