I am working on an OS X application in Xcode and I need to add a new line to a wrapping text field with code, but I've looked everywhere and even experimented some myself but I haven't been able to figure it out. I know when adding text to a wrapping text field that the text field will automatically add a new line when you've run out of space on the first line, but I want to create a new line in my text field with code. Any help would be appreciated.
A newline character can be written \n in a string, e.g. "\n".
Related
Currently in my configuration, Sublime will always preserve a blank line on the bottom of every file when saving. Since the last line is blank, is there any option to hide the appropriate line number?
NOTE: If there was no option in Sublime, is it possible to write a custom extension or plugin to handle it?
Really appreciate for any help,
The only way to do this is to eliminate the last line by using the following setting:
"ensure_newline_at_eof_on_save": false
Sublime is programmed such that when a line is terminated by a newline character (hex 0A, ASCII character 10), the next line is shown, which intuitively is the correct thing to do. All text editors that I know of have this behavior. Therefore, the only way to "hide" the last line number in the gutter is to not have a newline character terminating the previous line.
I have an issue with getting the contents of a tkinter text box. If I paste information into it from Excel (for example), it always adds an empty line at the bottom. I would like to remove that line automatically. This is what I use to get the contents:
contents = inputText.get(1.0, "end-1c")
for line in contents.split("\n"):
line = line.strip()
I initially added a '[:-1]' on the end of that, which works, but only on text that is pasted into the text box. If you type text in manually, then obviously there's no trailing '\n' on the end, so it ends up removing the last character that was typed. Not good!
How can I get it to remove (or ignore) the trailing empty lines? The 'line = line.strip()' in the code further down seems to have no effect.
Thanks, Chris.
Your problem isn't with getting the text, your problem is that pasting seems to add more than you expect. To get text, regardless of how it ended up in the widget, you should use .get("1.0", "end-1c"). There is simply no way to know at the time you are getting the contents of the widget if any extra newlines came from the user directly or from a paste operation. To tkinter, text is text no matter how it was entered.
The only solution is to adjust the paste binding, and even that is problematic because I don't think tkinter has any way of knowing if the clipboard contents are from excel, or from some other source.
If all you really want is to ignore all trailing blank lines, call strip() on the data before calling split("\n")
Textbox was always inserts a blank line at the end. The problem was compounded when editing textbox and yet another blank line was inserted. However during edit you could remove blank lines so you can't use "end-1c" all the time.
The trick was to remove 1 or more extra blank lines after editing:
# Rebuild lyrics from textbox
self.work_lyrics_score = \
self.lyrics_score_box.get('1.0', tk.END)
while self.work_lyrics_score.endswith('\n\n'):
# Drop last blank line which textbox automatically inserts.
# User may have manually deleted during edit so don't always assume
self.work_lyrics_score = self.work_lyrics_score[:-1]
Note however this is with Linux. For Windows you might need something like:
while self.work_lyrics_score.endswith('\n\r\n\r'):
# Drop last blank line which textbox automatically inserts.
# User may have manually deleted during edit so don't always assume
self.work_lyrics_score = self.work_lyrics_score[:-2]
Or whatever the the control code is for DOS/Windows LF/CR (Line Feed / Carriage Return) typewriter days style is.
I've been playing around with NSTextView and have applied some paragraph styles to certain lines. However, when I type enter and get a new line, the attributes that I applied to one line are bleeding into the next.
I want to be able to apply a paragraph style to one line and have the next line be formatted in the default way. You can see what I mean from the screenshots.
When I add some spacing between paragraphs via NSParagraphStyle, the same spacing applies to the newline, which makes the whole thing look pretty shotty. Basically, I am looking for a way to reset the paragraph style for an empty line.
I have tried [MyTextView resetTypingAttributes:theAttributes] to no avail, since you first have to start typing for the new attributes to apply. Just to be clear, the line below the text in the screenshot is the cursor, which is really far down there as a result of the paragraph spacing.
Screenshot:
It seems that you have to use setTypingAttributes on the textview.
I see that there is an attribute name (NSParagraphStyleAttributeName) for applying paragraph styles to text in Cocoa. Text is stored in an NSAttributedString, but what defines a "paragraph" in that string -- is it the newline character \n? Other characters? If it's a \n, then how do you create a new line without starting a new paragraph. And lastly, when you attach a ParagraphStyle to the string, do you have to use the exact range of the whole paragraph, or can you put it on any sub-range in the paragraph. If it can be a sub-range, how does the system handle two or more ParagraphStyles on the same paragraph?
thanks,
Rob
I got an answer from Douglas on Apple's cocoa-dev mailing list:
http://lists.apple.com/archives/Cocoa-dev/2010/Dec/msg00347.html
I'll copy what he wrote here:
Any of the standard paragraph separators can be used (\n, \r, \r\n, Unicode paragraph separator). Use the Unicode line separator character to start a new line without a paragraph break. It is best to apply a paragraph style to an entire paragraph; if that is not done, then the paragraph style attribute will be automatically fixed at attribute fixing time so that it is constant over each paragraph range, because that is needed at layout time.
It might be worth mentioning that Apple's String Programming Guide has a section on Paragraph and Line Breaks, and that NSString will give you the paragraph ranges without your having to search for paragraph separators.
the best way in iOS from my experience is to use
#"\n\r";
i found inconsistent behaviour with just using \n or even 0x2029 (which should be the equivalent of NSParagraphSeparatorCharacter (not defined in iOS))
the problems emerged when using NSAttributedString and NSParagraphStyle... as when using the #"\n" or (0x2029) the setParagraphSpacing was ignored by other than the first paragraph... using #"\n\r" got the paragraphes right
How do I place a small paragraph within a text field or label? Do I use code for the new line? I'm using Interface Builder with Xcode.
You could also try using an Text View (NSTextView/UITextView) instead of a text field or a label.
You must mean more than one paragraph, because a single paragraph doesn't need a new line. Just copy it out of a text editor and paste it in.