How to "insert" text into UITextField? - cocoa-touch

I'm trying to insert a text into a UITextField. But by insert, I mean replacing the character at cursor position with the new one (insert button's functionality on windows).
If I manually replace the incoming string and do setText (with NO returned to shouldChangeCharactersInRange method), the cursor moves to the end of textField.

For UITextField (iOS 3.2+), try:
[textField replaceRange:[textField selectedTextRange] withText:toInsert];

You can replace the text as you have done previously and restore the position of your caret. Note that this can only be done with UITextView rather than UITextField (that would require a lot more unjustified work, unless you absolutely have to use UITextField):
NSRange selectionRange = myTextView.selectedRange;
myTextView.text = myNewText;
myTextView.selectedRange = selectionRange;
You have to note that it's a good idea to check the validity of selectionRange variable for new text - make sure that cursor position isn't greater than the new text length.

Related

Word wrap position in UILabel

When working with a muti-line UILabel object, is there a way to know at which position in the text the line wrapping will occur?
For example if the text of my label is 20 words, the display will be different according to the size of the box provided. I would like to know when (character position) the display is going to the next line.

Deleting a line of text on an NSTextView

I have a NSTextView called TheTextView. I'm adding there lines of text, each line representing a full path to a file with a \n at the end. The textview is set to not editable. I add or remove text via a + and - buttons.
Supposedly the user can select a line and delete it. I've tried with
[TheTextView delete:nil];
But nothing happens. How can I delete the selected line? And while we are in this topic, is there any way to set the TextView to select the whole line when I click on one line?
Thanks
EDIT:
OK, so I found that if I do the following
[TheTextView setEditable:YES];
[TheTextView delete:nil];
[TheTextView setEditable:NO];
then it works. Now I need to know how to select the entire line when the user click it because otherwise it will leave a blank space in between the two lines where the one deleted was. Ideas?
It sounds like no text is selected when you call [TheTextView delete:nil]. Try calling setSelectedRange: with a range to select a line of text, then try calling delete:. For example:
[TheTextView setSelectedRange:NSMakeRange(0,24)];
[TheTextView delete:nil];
Of course, you'll need to determine the correct start and length of your range to correspond the text you want to delete.

UITextView setText works only once

I have a UITextView in my xib and in my UIViewController class, I call setText on this element. It works correctly - once.
Subsequently, it does not set the text and the old text remains.
I also have the same problem with UINavigationBar, where navBar.topItem.title can be set once but not again.
How do I clear the old values and get the new values to show?
Thanks
DeShawn
Does 'subsequently' mean 'on the next line of code' or 'the next time a button is clicked' ?
The latter is appropriate I guess, don't you set your outlet to nil somewhere ?

Uitableview cell text got hidden on edit Button

When i try to delete any row - while clicking the edit button - the delete button appears and the text on right side truncated from the end.
I want to compress it automatically while delete button appears.
You can try setting the textLabel property attributes to automatically truncate the middle of the string or set a smaller (minimum) font.
[[theCell textLabel] setMinimumFontSize:theSizeYouWant];
or
[[theCell textLabel] setLineBreakMode:UILineBreakModeMiddleTruncation];

How can I force entry to complete in a NSTextField from a NSButton

I just noticed a problem when my user-interface is in a certain state. I have a table view with two columns both of which the user can enter data that is to be used later. There's also a button which acts upon the contents of the table view.
The problem is if the user has entered new data in the column but has not yet exited the field by using the tab key or return key (i.e. the cursor is still in the field and in editing mode) and the button is pressed the old value is used not the current value sitting in the field.
What is the best way to handle this this? I want to use whatever the user has entered thus far.
Basically, The button code needs to tell the text field to finish completion or exit the editing mode. But I can't seem to find a method that will do that.
Use bindings. In Interface Builder, select the table column and in the Inspector go to Table Column Bindings and set the Value content binding appropriately and ensure the "Continuously Updates Values" option is checked. Then changes to the table cell content will propagate immediately.
Found the answer, At least for me.
Find out if a row is selected and if so deselect it. This causes the current entry to be completed.
- (void) completeTableEntry
{
// If a column is selected ensure it is completed
NSInteger sr = [keyValueTable selectedRow];
if (sr != -1) {
[keyValueTable deselectRow:sr];
}
}
How about:
[theTargetWindowWhateverThatIs endEditingFor:nil];
theTargetWindowWhateverThatIs may be, for example, self.window if you are inside a NSWindowController.