I retrieve an NSString from a Property list and display it in a UILabel. The NSString already includes \n s, however the UILabel just displays them as text. How can I tell the UILabel to actually use the \n s as line breaks?
Everything you type into a plist in the plist editor is interpreted as plain text. Try it... put a ' into a field and right click -> view as "plain text" and you'll see it substitutes it for '. Therefore you can't put \n into a plist because it thinks you're just typing text and will treat it as such. Instead of putting \n into your plist use Alt+Enter to get your newline. If you view this as a text file now you'll see \ns printed and new lines acctually shown in the text file.
Now when you output it it won't display \n it will just give it a new line.
Plus, as has been mentioned UITextField is only one line anyway and you probably would benefit from using UITextView.
Well, first, you are going to need a string that you can modify. To accomplish that, you can simply do:
NSMutableString* correctedPath = [path mutableCopy];
At that point, you can use -insertString:atIndex: to insert any characters you need.
You're using the wrong class here.
UITextField doesn't (for all that I know) support multi-line input. For that, you will need a UITextView (it has editing enabled by default). It should interpret \n's without any problems. It also has a lineBreakMode property, if you want to make use of that.
Related
Under certain circumstances, UILabel seems to bring an extra word to new line even when there is enough space for it, for example,
If one more word is appended,
Even if I force the width of the label to become something like below, it still moves the word consists of "c"s to the next line,
I've tried twisting the configuration of the UILabel, but seems it behaves the same unless I set the line breaking mode to character wrap, below is the configuration for the above cases,
And the constraints (in the first two cases, the trailing ),
Is there any reason for this particular behaviour and can I fix this? It looks weird in this way leaving that space emptied.
this is the default behavior since iOS 11, to fix orphaned words. No way to shut it off
to fix it
use the text as attributed text
or
use UItextview and turn of the scroll, edit option
or
use the custom label here
Get each line of text in a UILabel
You should set the line break to character wrap, because the letters after the space will be recognized as a word.
Hey I know this is late but I just figured out that you can cheat the system by adding a bunch of spaces at the end of the text.
If text of UILable may be changed, then it's still possible to use quick-dirty hack with "\n" - new line symbol.
Text "Aaaaaaaaaaaaaa bbb cccccccccc\ndddddd" will force UILabel to display:
Aaaaaaaaaaaaaa bbb cccccccccc
ddddddd
In Interface Builder new line can be inputted with Ctrl + Enter
If u use wordWrap, it tries to keep words in full form, as a result of this, it goes to next line. If you use character wrap, it will break on characters, and push the chars onto next line.
For Example:-
My name is ABCXXXX and I (have space here)
love myself.
Solution:-
Use NSMutableAttributedText and write love\n. It will make the "love" be in the space myself in the next line.
I use UITextView to hold some infomation, and I have edited the text in the .xib file. The line will be changed when the current line has no space. The problem is that I don`t not how to change line when I want to. Look at the pic below:
I have try to use "\n" or "\\n", but it is displayed as text. I want to know how can I change line in the .xib file?
this should work...
textView.text=#"Ankit \r Srivastava";
this should give you both the words in different rows...
I am going to develop the iPhone app. But I got stuck with one place. I want to write some symbol on the label from the xib file.
The symbols are not on the keyboard but we can get it by the ASCII value.
e.g: the ACSII value for the character sign "mue" is 230 but how to print that symbol "mue" on the label that i dont know.
So please help me for that.
Thanks in advance.
Use whatever editor you like to produce that character, and open your xib in XCode and just copy/paste it in?
Use NSUTF8StringEncoding to encode your string.
For example,
NSString *str = [NSString stringWithUTF8String:"your string for encoding"];
[lblName setText:str];
Following function will also help :
- (NSString *) decodedString:(NSString *) originalString {
NSString *newString = [NSString stringWithUTF8String:[originalString cStringUsingEncoding:[NSString defaultCStringEncoding]]];
return newString;
}
I suggest to use above function.
Pressing Cmd+Ctrl+Space will open a special characters menu. Check if the desired symbol is present. If it isn't, click the gear icon, then select the desired category — add it to the list.
See screenshot below
I think you mean the character 'µ', yes?
If so, you can simply type it into the label by clicking "option" and "m" on your Macintosh keyboard, when you are editing the label in your XIB.
If you can use the unicode number instead, you can do it like this:
NSString *muString = [NSString stringWithFormat:#"%C", 0x03BC];
Write 0x then the unicode number.
Open the "Special Characters"-Panel and search for your character. You can find it at the bottom of the edit menu. There is a shortcut for it too, cmd+opt+t
Copy and paste your character from there to your UILabel.
and btw: option + m = µ
You can simply type into label by using 'Alt' key + 'm' key on key board, when you are editing the label in your XIB and rest of symbols, you can get easily using 'Alt' key and other keys. You can fix 'Alt' key and change other keys(one by one).
I am working on a math app and need to output exponents to the screen.
I've found that this code will work:
NSLog(#"x\u2070 x\u00B9 x\u00B2 x\u00B3 x\u2074 x\u2075 x\u2076 x\u2077 x\u2078 x\u2079");
it displays: x⁰ x¹ x² x³ x⁴ x⁵ x⁶ x⁷ x⁸ x⁹
This also works:
NSString *testString = #"8.33x10\u00B3";
NSLog(#"test string: %#", testString);
it displays: test string: 8.33x10³
Even setting it to a label displays correctly on the iPhone screen:
NSString *testString = #"8.33x10\u00B3";
Answer1Label.text = testString;
However, when I pull the string from a .plist that says "8.33x10\u00B3" and display it on the screen, it just shows up as "8.33x10\u00B3" instead of 8.33x10³
Is there an additional character I need to put in front of the \u00B3 to get it to recognize?
Thanks for your help!
The \uXXXX is converted into unicode at compile time, so you wouldn't expect that to be magically converted by reading a .plist.
Try opening the the plist file in Xcode in "text mode" (right click your plist file, Open As -> Plan Text File), then edit the desired string to contain the special characters by using text of the form:
⁰
rather than the usual \u2070 you've been using in-code. Then if you save your plist, close it, and open it again by double clicking, you'll see the usual plist editor view and it will contain your special characters.
Alternatively, consider using OS X's character viewer (aka character palette) to input the text directly into the plist editor in Xcode. More info.
Would I use a UILabel for that? Or is there something better?
It depends on what you want to achieve. If you just want a multi-line label, then you can use UILabel with the numberOfLines parameter set to something other than 1 (set it to zero if you don't care how many lines are used). If you need to let the user edit the text, then a UITextView is the way forward. Line breaks are indicated using the \n character, not the /n sequence as Emil incorrectly wrote.
You can use a UITextView, and write \n where you want the new lines.
Hey!\nHow are you?\n\n\nYOU: I am fine.\nME: That's great! will display:
Hey!
How are you?
YOU: I am fine
ME: That's great!