Adding a method to my uitextfield in cell? - iphone

I am adding this Method to my code to format the textfield. I am using the code below to try and add the method, but it not working, what am I doing wrong?
.h file
NSString* phone_;
UITextField* phoneFieldTextField;
#property (nonatomic,copy) NSString* phone;
.m file
#synthesize phone = phone_;
ViewDidLoad{
self.phone = #"";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// Make cell unselectable and set font.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:#"ArialMT" size:13];
if (indexPath.section == 0) {
UITextField* tf = nil;
switch ( indexPath.row ) {
case 3: {
cell.textLabel.text = #"Phone" ;
tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:#"xxx-xxx-xxxx"];
phoneFieldTextField.keyboardType = UIKeyboardTypePhonePad;
[self formatPhoneNumber:phoneFieldTextField.text deleteLastChar:YES];
[cell addSubview:phoneFieldTextField];
break ;
}
// Textfield dimensions
tf.frame = CGRectMake(120, 12, 170, 30);
// Workaround to dismiss keyboard when Done/Return is tapped
[tf addTarget:self action:#selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
}
}
// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {
//Section 1.
if ( textField == nameFieldTextField ) {
self.name = textField.text ;
} else if ( textField == addressFieldTextField ) {
self.address = textField.text ;
} else if ( textField == emailFieldTextField ) {
self.email = textField.text ;
} else if ( textField == phoneFieldTextField ) {
self.phone = textField.text ;
}else if ( textField == dateOfBirthTextField ) {
self.dateOfBirth = textField.text ;
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* totalString = [NSString stringWithFormat:#"%#%#",textField.text,string];
// if it's the phone number textfield format it.
if(textField.tag == 10 ) {
if (range.length == 1) {
// Delete button was hit.. so tell the method to delete the last char.
textField.text = [self formatPhoneNumber:totalString deleteLastChar:YES];
} else {
textField.text = [self formatPhoneNumber:totalString deleteLastChar:NO ];
}
return false;
}
return YES;
NSLog(#"Testing should change character in range");
}
-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar {
if(simpleNumber.length == 0) return #"";
// use regex to remove non-digits(including spaces) so we are left with just the numbers
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"[\\s-\\(\\)]" options:NSRegularExpressionCaseInsensitive error:&error];
simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:#""];
// check if the number is to long
if(simpleNumber.length>10) {
// remove last extra chars.
simpleNumber = [simpleNumber substringToIndex:10];
}
if(deleteLastChar) {
// should we delete the last digit?
simpleNumber = [simpleNumber substringToIndex:[simpleNumber length] - 1];
}
// 123 456 7890
// format the number.. if it's less then 7 digits.. then use this regex.
if(simpleNumber.length<7)
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:#"(\\d{3})(\\d+)"
withString:#"($1) $2"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
else // else do this one..
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:#"(\\d{3})(\\d{3})(\\d+)"
withString:#"($1) $2-$3"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
if (simpleNumber.length == 10 && deleteLastChar == NO) { [self resignFirstResponder];}
return simpleNumber;
NSLog(#"Testing format phone number");
}
#pragma mark - TextField
-(UITextField*) makeTextField: (NSString*)text
placeholder: (NSString*)placeholder {
UITextField *tf = [[UITextField alloc] init];
tf.placeholder = placeholder;
tf.text = text ;
tf.autocorrectionType = UITextAutocorrectionTypeNo ;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.adjustsFontSizeToFitWidth = YES;
tf.returnKeyType = UIReturnKeyDone;
tf.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
return tf ;
}

The method you are using:
-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar
Returns an NSString Object. In your case you are calling the method correctly but you are not setting the Returned NSString object to anything. It is simply hanging there. You need to set the phoneFieldTextField to the formatted text like so:
phoneFieldTextField.text = [self formatPhoneNumber:phoneFieldTextField.text deleteLastChar:YES];
NOTE - If you want to learn more about return methods then read the following:
If you noticed some most methods are of the void type. You know this when you see a method like this:
- (void)someMethod {
int x = 10;
}
What void means is that the someMethod does not return anything to you. It simply executes the code within the method. Now methods than return an object or some other data type look like this:
- (int)returnSomething {
int x = 10;
return x;
}
First thing you will notice is the return type is no longer void, it is an int. This means the method will return an integer type. In this case the code executes and you are returned the value of x.
This is just the start of the topic of return methods but hopefully it makes things a bit clearer for you.

First off you need to tell us What is not working we don't have your app and all your code. You need to explain what is working and what is not working exactly. It took longer then necessary to figure out that your question is why is textField:shouldChangeCharactersInRange: not working. Did you set a breakpoint in the function to see what it was doing. Was it not being called?
That said your bug is that textField:shouldChangeCharactersInRange: is using tags to identify text fields but the rest of the code is using pointers
// if it's the phone number textfield format it.
- if(textField.tag == 10 ) {
+ if(textField.tag == phoneFieldTextField ) {
Also you didn't include the code for makeTextField:placeholder: There could be issues in it too. Compare your code to the makeTextField:placeholder: in my sample.
I created a sample project on GitHub. To fix this. I also demos a better approach to creating input forms using table views.
https://github.com/GayleDDS/TestTableViewTextField.git
Look at both diffs to see what I did to YourTableViewController.m to make things work.
https://github.com/GayleDDS/TestTableViewTextField/commit/d65a288cb4da7e1e5b05790ea23d72d472564793
https://github.com/GayleDDS/TestTableViewTextField/commit/31ecaec8c9c01204643d72d6c3ca5a4c58982099
There is a bunch of other Issues here:
You need to call [super viewDidLoad]; in your viewDidLoad method
You need to correctly indent your code (could be a cut and paste issue)
You should be using the storyboard to create your views. See the better solution tab and BetterTableViewController implementation.
Must Watch - iOS Development Videos
WWDC 2011 - Session 309 - Introducing Interface Builder Storyboarding
https://developer.apple.com/videos/wwdc/2011/?id=309
Stanford iPhone Programing Class (Winter 2013)
Coding Together: Developing Apps for iPhone and iPad
https://itunes.apple.com/us/course/coding-together-developing/id593208016
Lecture 9. Scroll View and Table View
Lecture 16. Segues and Text Fields

Looks like you are not setting the delegate <UITextFieldDelegate> in the .h file, and not assigning your textfield's delegate property to self tf.delegate = self; in order to call - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Try that and let me know how it goes
-Good Luck!

#koray was right: you need to setup the delegate for the class. Your class should be declared as implementing the protocol UITextFieldDelegate (in addition to UITableViewDataSource, I assume)
then in your makeTextField: (NSString*)text placeholder: (NSString*)placeholder method, you need to have something like:
-(UITextField*) makeTextField: (NSString*)text
placeholder: (NSString*)placeholder {
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 150, 40)];
tf.placeholder = placeholder;
// (...)
tf.delegate = self;
return tf ;
}
Then you need to setup the delegate methods correctly. In the following example, I have a nav bar, since the numbers pad doesn't have a return or a done button. I setup a button that will act as the done button (you may have another way of making the keyboard go, and switching between text fields will trigger the end of edition anyway):
- (void) textFieldDidBeginEditing:(UITextField *)textField {
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneEditing:)];
self.navBar.topItem.rightBarButtonItem = doneBtn;
}
- (void) doneEditing:(id) sender {
if(phoneFieldTextField.isFirstResponder) {
[phoneFieldTextField resignFirstResponder];
}
// (...)
self.navBar.topItem.rightBarButtonItem = nil;
}
Then, the magic happens in the textDidEndEditing delegate method:
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ( textField == phoneFieldTextField ) {
self.phone = [self formatPhoneNumber:textField.text deleteLastChar:YES] ; // convert
[phoneFieldTextField setText:self.phone]; // display
}
// (...)
}

Related

How to add views between UICollectionViewCells in a UICollectionView?

I'm trying to add UIViews between my UICollectionViewCells in my UICollectionView and I don't know how I could do that. I'm trying to accomplish something like this:
I'll probably need to write a custom UICollectionViewLayout, but I don't really know where to start.
I studied more of how UICollectionViewLayouts work and figured out how to solve it. I have an UICollectionReusableView subclass called OrangeView that will be positioned between my views, than I wrote an UICollectionViewFlowLayout subclass called CategoriesLayout that will deal with my layout.
Sorry for the big block of code, but here is how it looks like:
#implementation CategoriesLayout
- (void)prepareLayout {
// Registers my decoration views.
[self registerClass:[OrangeView class] forDecorationViewOfKind:#"Vertical"];
[self registerClass:[OrangeView class] forDecorationViewOfKind:#"Horizontal"];
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath {
// Prepare some variables.
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:indexPath.row+1 inSection:indexPath.section];
UICollectionViewLayoutAttributes *cellAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *nextCellAttributes = [self layoutAttributesForItemAtIndexPath:nextIndexPath];
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
CGRect baseFrame = cellAttributes.frame;
CGRect nextFrame = nextCellAttributes.frame;
CGFloat strokeWidth = 4;
CGFloat spaceToNextItem = 0;
if (nextFrame.origin.y == baseFrame.origin.y)
spaceToNextItem = (nextFrame.origin.x - baseFrame.origin.x - baseFrame.size.width);
if ([decorationViewKind isEqualToString:#"Vertical"]) {
CGFloat padding = 10;
// Positions the vertical line for this item.
CGFloat x = baseFrame.origin.x + baseFrame.size.width + (spaceToNextItem - strokeWidth)/2;
layoutAttributes.frame = CGRectMake(x,
baseFrame.origin.y + padding,
strokeWidth,
baseFrame.size.height - padding*2);
} else {
// Positions the horizontal line for this item.
layoutAttributes.frame = CGRectMake(baseFrame.origin.x,
baseFrame.origin.y + baseFrame.size.height,
baseFrame.size.width + spaceToNextItem,
strokeWidth);
}
layoutAttributes.zIndex = -1;
return layoutAttributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *baseLayoutAttributes = [super layoutAttributesForElementsInRect:rect];
NSMutableArray * layoutAttributes = [baseLayoutAttributes mutableCopy];
for (UICollectionViewLayoutAttributes *thisLayoutItem in baseLayoutAttributes) {
if (thisLayoutItem.representedElementCategory == UICollectionElementCategoryCell) {
// Adds vertical lines when the item isn't the last in a section or in line.
if (!([self indexPathLastInSection:thisLayoutItem.indexPath] ||
[self indexPathLastInLine:thisLayoutItem.indexPath])) {
UICollectionViewLayoutAttributes *newLayoutItem = [self layoutAttributesForDecorationViewOfKind:#"Vertical" atIndexPath:thisLayoutItem.indexPath];
[layoutAttributes addObject:newLayoutItem];
}
// Adds horizontal lines when the item isn't in the last line.
if (![self indexPathInLastLine:thisLayoutItem.indexPath]) {
UICollectionViewLayoutAttributes *newHorizontalLayoutItem = [self layoutAttributesForDecorationViewOfKind:#"Horizontal" atIndexPath:thisLayoutItem.indexPath];
[layoutAttributes addObject:newHorizontalLayoutItem];
}
}
}
return layoutAttributes;
}
#end
I also wrote a category with some methods to check if an index path is the last in a line, in the last line or the last in a section:
#implementation UICollectionViewFlowLayout (Helpers)
- (BOOL)indexPathLastInSection:(NSIndexPath *)indexPath {
NSInteger lastItem = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:indexPath.section] -1;
return lastItem == indexPath.row;
}
- (BOOL)indexPathInLastLine:(NSIndexPath *)indexPath {
NSInteger lastItemRow = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:indexPath.section] -1;
NSIndexPath *lastItem = [NSIndexPath indexPathForItem:lastItemRow inSection:indexPath.section];
UICollectionViewLayoutAttributes *lastItemAttributes = [self layoutAttributesForItemAtIndexPath:lastItem];
UICollectionViewLayoutAttributes *thisItemAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
return lastItemAttributes.frame.origin.y == thisItemAttributes.frame.origin.y;
}
- (BOOL)indexPathLastInLine:(NSIndexPath *)indexPath {
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:indexPath.row+1 inSection:indexPath.section];
UICollectionViewLayoutAttributes *cellAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *nextCellAttributes = [self layoutAttributesForItemAtIndexPath:nextIndexPath];
return !(cellAttributes.frame.origin.y == nextCellAttributes.frame.origin.y);
}
#end
And this is the final result:
Looks like if your collectionView background was green and contentView white you could get the horizontals with a space between the cells minimumLineSpacing. The vertical gap would be the tricky part, but if you were creative with your contentView and set the minimumInteritemSpacing carefully you could get it.
If you're using sections and the layout is appropriate for it, you might use section headers and footers.
But based on your illustration, it looks like you just need to define the UICollectionViewCell to contain those views. So, where you register your class:
[collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
put those border images in the UICollectionView cell subclass (in the above case, "CollectionViewCell"). That seems like the easiest approach.
Here's one I use:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.restorationIdentifier = #"Cell";
self.backgroundColor = [UIColor clearColor];
self.autoresizingMask = UIViewAutoresizingNone;
const CGFloat borderWidth = 3.0f;
UIView *bgView = [[UIView alloc] initWithFrame:frame];
bgView.layer.borderColor = [UIColor blackColor].CGColor;
bgView.layer.borderWidth = borderWidth;
bgView.layer.cornerRadius = 6.0f;
self.selectedBackgroundView = bgView;
}
return self;
}
Wow. That's a lot of code in the other answers just for a separator line between rows..
This is how I solved it. First you'll need to add the line separator inside the cell. Make sure you keep dragging it making it wider than the actual cell width so if your cell width is 60p your separator line will be 70.
#implementation CollectionViewController
{
NSArray *test;
int currentLocInRow;
}
inside cellForItemAtIndexPath:
if((indexPath.row+1) % 4 == 0)
{
cell.clipsToBounds = YES;
}
else
{
cell.clipsToBounds = NO;
}
currentLocInRow++;
if([test count] - indexPath.row+1 < 4 - currentLocInRow)
{
cell.lineSeparator.alpha = 0;
}
else
{
cell.lineSeparator.alpha = 1;
}
if(currentLocInRow==4)currentLocInRow=0;
If you want a separator at the end of the collection view but there's a chance that you won't get 4 cells at the last row you can add a simple Collection Reusable View as Footer.

Check if any textfield, textview or label changed for a UIViewController..?

I have a view controller and it contains n number of UITextFields and UItextViews and UILabels, is there anyhow I can get notified if any of them changes..?
I can do it by manually looking at each of them in their respective TextDidChangeNotification and similar but I am looking for a more optimized way of doing this, where I don't have to worry about each of them .
// Assumes you don't use tag values now - if you do small change to create
// and index set, add the ones you use, so all new ones assigned are unique.
// assumes ARC
1) New ivar:
{
NSMutableDictionary *savedValues;
}
1) When you want to baseline the values:
savedValues = [self snapshot];
2) call this to baseline current values initially then at any later point:
- (NSMutableDictionary *)snapshot
{
NSInteger tag = 1;
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[self.view.subviews count]];
for(UIView *v in self.view.subviews) {
if([v isKindOfClass:[UILabel class]] || [v isKindOfClass:[UITextField class]] || [v isKindOfClass:[UITextView class]]) {
if(v.tag == 0) v.tag = tag++; // will happen once
// EDIT below
    [dict setObject:[NSString stringWithString:[(id)v text]] forKey:[NSNumber numberWithInteger:tag]];
}
}
return dict;
}
4) When you want to see if anything changed:
- (BOOL)anyChanges
{
NSDictionary *currentDict = [self snapshot];
return [currentDict isEqualToDictionary:savedValues];
}
You should use one method for all textFields, textView and Labels. and give a unique tag to textFields and textViews and labels. which help you for define it's textField ,textView or Label.
if(sender.tag == 1000)//UILabel
{
UILabel *label=(UILabel *)sender
//write own code for label what do you want.
}
else if(sender.tag == 2000)//UITextField
{
UITextField *textField=(UITextField *)sender
//write own code for textField what do you want.
}
else if(sender.tag == 3000)// UITextView
{
UITextView *textView=(UITextView *)sender
//write own code for textView what do you want.
}
Make your class to handle UIControlEventValueChanged event in your textfield, textview or label's value is changed. Add this line ViewDidLoad method:
[youLabel addTarget:self action:#selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
// add for textfield, textview also if needed
[youTextField addTarget:self action:#selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
[youTextView addTarget:self action:#selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
Now selector would be called when ever value is changed
- (void) valueChanged:(id)sender{
if(sender isKindofClass[UILabel class])
{
//label value changed here to differntiate used tag
if([sender tag] == 0)
....
....
}
else if(sender isKindofClass[UITextField class])
{
// textField value changed to differntiate used tag
if([sender tag] == 0)
....
....
}
else if(sender isKindofClass[UITextView class])
{
// textview value changed to differntiate used tag
if([sender tag] == 0)
....
....
}
}

How can I force the keyboard to stay displayed?

I have an IBAction that is called when someone is done entering text in a field. I then validate the input. If I have determined there is an error, I display a message and want the user to enter into that same field again. Rather than make them select the text field to bring the keyboard up (which works fine) I want to just leave the keyboard displayed.
I am doing [SymbolEntered becomeFirstResponder]; as the last statement in my IBAction, but the keyboard still goes away. Am I putting that in the wrong place? Any help would be appreciated. Thanks.
- (IBAction)textFieldDoneEditing:(id)sender {
DebugMsg.text = nil;
DebugMsg2.text = nil;
DebugMsg3.text = nil;
NSLog (#"done editing");
NSLog (#"%#", SymbolEntered.text);
if ([SymbolEntered.text isEqualToString:nil])
{
Result.textColor = [UIColor redColor];
Result.text = #"You must enter a symbol!";
[SymbolEntered becomeFirstResponder];
}
else
{
if ([SymbolEntered.text isEqualToString:
[NSString stringWithCString:elements_table2[el_tbl_idx-1].element_symbol]])
{
correct_count++;
Result.textColor = [UIColor greenColor];
Result.text = #"Correct!";
Score.hidden = FALSE;
Score.text = [NSString stringWithFormat:#"Score: %d out of %d - %d Percent", correct_count, el_count+1,
(correct_count*100)/(el_count+1)];
GetNextElementButton.hidden = FALSE;
SymbolEntered.enabled = FALSE;
el_count++;
attempts = max_tries + 1;
}
else
{
Score.hidden = TRUE;
Result.textColor = [UIColor redColor];
if (attempts < max_tries)
{
if (attempts+1 == max_tries)
{
Result.text = #"Sorry, one more try -";
}
else
{
Result.text = #"Sorry, try again - ";
}
GetNextElementButton.hidden = TRUE;
attempts++;
}
else
{
Result.text = [[NSString alloc] initWithFormat: #"Sorry. The correct answer is %#",
[NSString stringWithCString:elements_table2[el_tbl_idx-1].element_symbol]];
Score.hidden = FALSE;
Score.text = [NSString stringWithFormat:#"Score: %d out of %d - %d Percent", correct_count, el_count+1, (correct_count*100)/(el_count+1)];
GetNextElementButton.hidden = FALSE;
SymbolEntered.enabled = FALSE;
el_count++;
}
}
}
[SymbolEntered becomeFirstResponder];
NSLog (#"end of textfieldoneediting");
}
You can do your validation in the UITextField textFieldShouldEndEditing: delegate method instead. If you return NO from that callback, the text field will remain first responder and the keyboard won't go away. (You'll have to make your controller object the text field's delegate if it isn't already, of course.)
Try calling the [textField becomeFirstResponder] sometime later? Also make sure the UITextField pointer is not nil or something. For more help please show some of your code, it's very hard to tell where the problem is like this.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Try This it will resign the keyboard.

Limiting pasted string length in UITextView or UITextField

The problem of limiting strings that are directly entered into a UITextView or UITextField has been addressed on SO before:
iPhone SDK: Set Max Character length TextField
iPhone sdk 3.0 issue
However now with OS 3.0 copy-and-paste becomes an issue, as the solutions in the above SO questions don’t prevent pasting additional characters (i.e. you cannot type more than 10 characters into a field that is configured with the above solutions but you can easily paste 100 characters into the same field).
Is there a means of preventing directly entered string and pasted string overflow?
I was able to restrict entered and pasted text by conforming to the textViewDidChange: method within the UITextViewDelegate protocol.
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length >= 10)
{
textView.text = [textView.text substringToIndex:10];
}
}
But I still consider this kind of an ugly hack, and it seems Apple should have provided some kind of "maxLength" property of UITextFields and UITextViews.
If anyone is aware of a better solution, please do tell.
In my experience just implementing the delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
works with pasting. The entire pasted string comes across in the replacementString: argument. Just check it's length, and if it's longer than your max length, then just return NO from this delegate method. This causes nothing to be pasted. Alternatively you could substring it like the earlier answer suggested, but this works to prevent the paste at all if it's too long, if that's what you want.
Changing the text after it's inserted in textViewDidChange: causes the app to crash if the user presses 'Undo' after the paste.
I played around for quite a bit and was able to get a working solution. Basically the logic is, do not allow the paste if the total length is greater than the max characters, detect the amount that is overflown and insert only the partial string.
Using this solution your pasteboard and undo manager will work as expected.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSInteger newLength = textView.text.length - range.length + text.length;
if (newLength > MAX_LENGTH) {
NSInteger overflow = newLength - MAX_LENGTH;
dispatch_async(dispatch_get_main_queue(), ^{
UITextPosition *start = [textView positionFromPosition:nil offset:range.location];
UITextPosition *end = [textView positionFromPosition:nil offset:NSMaxRange(range)];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
[textView replaceRange:textRange withText:[text substringToIndex:text.length - overflow]];
});
return NO;
}
return YES;
}
This code won't let user to input more characters than maxCharacters.
Paste command will do nothing, if pasted text will exceed this limit.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
return newText.count <= maxCharacters;
}
One of the answers in the first question you linked above to should work, namely using something like
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(limitTextField:) name:#"UITextFieldTextDidChangeNotification" object:myTextField];
to watch for changes to the text in the UITextField and shorten it when appropriate.
Also, string length as in '[string length]' is one thing, but one often needs to truncate to a byte count in a certain encoding. I needed to truncate typing and pasting into a UITextView to a max UTF8 count, here's how I did it. (Doing something similar for UITextField is an exercise to the reader.)
NSString+TruncateUTF8.h
#import <Foundation/Foundation.h>
#interface NSString (TruncateUTF8)
- (NSString *)stringTruncatedToMaxUTF8ByteCount:(NSUInteger)maxCount;
#end
NSString+TruncateUTF8.m
#import "NSString+TruncateUTF8.h"
#implementation NSString (TruncateUTF8)
- (NSString *)stringTruncatedToMaxUTF8ByteCount:(NSUInteger)maxCount {
NSRange truncatedRange = (NSRange){0, MIN(maxCount, self.length)};
NSInteger byteCount;
// subtract from this range to account for the difference between NSString's
// length and the string byte count in utf8 encoding
do {
NSString *truncatedText = [self substringWithRange:truncatedRange];
byteCount = [truncatedText lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if (byteCount > maxCount) {
// what do we subtract from the length to account for this excess count?
// not the count itself, because the length isn't in bytes but utf16 units
// one of which might correspond to 4 utf8 bytes (i think)
NSUInteger excess = byteCount - maxCount;
truncatedRange.length -= ceil(excess / 4.0);
continue;
}
} while (byteCount > maxCount);
// subtract more from this range so it ends at a grapheme cluster boundary
for (; truncatedRange.length > 0; truncatedRange.length -= 1) {
NSRange revisedRange = [self rangeOfComposedCharacterSequencesForRange:truncatedRange];
if (revisedRange.length == truncatedRange.length)
break;
}
return (truncatedRange.length < self.length) ? [self substringWithRange:truncatedRange] : self;
}
#end
// tested using:
// NSString *utf8TestString = #"Hello world, Καλημέρα κόσμε, コンニチハ ∀x∈ℝ ıntəˈnæʃənəl ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈ STARGΛ̊TE γνωρίζω გთხოვთ Зарегистрируйтесь ๏ แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช ሰማይ አይታረስ ንጉሥ አይከሰስ። ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌ ░░▒▒▓▓██ ▁▂▃▄▅▆▇█";
// NSString *truncatedString;
// NSUInteger byteCount = [utf8TestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
// NSLog(#"length %d: %p %#", (int)byteCount, utf8TestString, utf8TestString);
// for (; byteCount > 0; --byteCount) {
// truncatedString = [utf8TestString stringTruncatedToMaxUTF8ByteCount:byteCount];
// NSLog(#"truncate to length %d: %p %# (%d)", (int)byteCount, truncatedString, truncatedString, (int)[truncatedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
// }
MyViewController.m
#import "NSString+TruncateUTF8.h"
...
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)replacementText
{
NSMutableString *newText = textView.text.mutableCopy;
[newText replaceCharactersInRange:range withString:replacementText];
// if making string larger then potentially reject
NSUInteger replacementTextLength = replacementText.length;
if (self.maxByteCount > 0 && replacementTextLength > range.length) {
// reject if too long and adding just 1 character
if (replacementTextLength == 1 && [newText lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > self.maxByteCount) {
return NO;
}
// if adding multiple charaters, ie. pasting, don't reject altogether but instead return YES
// to accept and truncate immediately after, see http://stackoverflow.com/a/23155325/592739
if (replacementTextLength > 1) {
NSString *truncatedText = [newText stringTruncatedToMaxUTF8ByteCount:self.maxByteCount]; // returns same string if truncation needed
if (truncatedText != newText) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0LL), dispatch_get_main_queue(), ^{
UITextPosition *replaceStart = [textView positionFromPosition:textView.beginningOfDocument offset:range.location];
UITextRange *textRange = [textView textRangeFromPosition:replaceStart toPosition:textView.endOfDocument];
[textView replaceRange:textRange withText:[truncatedText substringFromIndex:range.location]];
self.rowDescriptor.value = (truncatedText.length > 0) ? truncatedText : nil;
});
}
}
}
[self updatedFieldWithString:(newText.length > 0) ? newText : nil]; // my method
return YES;
}
You can know the pasted string if you check for string.length in shouldChangeCharactersIn range: delegate method
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.length > 1 {
//pasted string
// do you stuff like trim
} else {
//typed string
}
return true
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if(string.length>10){
return NO;
}
return YES;
}

Multi-line TextField (similar to SMS) and / or 'Done' button in UITextView

I've been researching this for a few days now, and would appreciate a little help. Is there any way to generate a multi-line UITextField like Apple use in the SMS application? The useful thing about this control is that it has the 'sunk' appearance that makes it clear that it is a text entry box, but at the same time, it expands on each new-line character.
Failing that, if I'm forced to use a UITextView, can anyone advise how best to dismiss the keyboard ? Both the 'Done' and the 'Go' buttons just appear to generate newline characters ('\n'). This seems wrong to me - surely at least one of these should generate a different character, so that I can still allow for newline characters, but also dismiss my keyboard on a specific key press.
Am I missing something simple here ?
Thanks in advance :)
Maybe you can build upon a class I wrote? It's the same as tttexteditor, without the ugly glitches: http://www.hanspinckaers.com/multi-line-uitextview-similar-to-sms
An old question, but after several hours I've figured out how to make it the same perfectly as in Instagram (it has the best algorithm among all BTW)
Initialize with this:
// Input
_inputBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, size.height - _InputBarHeight, size.width, _InputBarHeight)];
_inputBackgroundView.autoresizingMask = UIViewAutoresizingNone;
_inputBackgroundView.contentMode = UIViewContentModeScaleToFill;
_inputBackgroundView.userInteractionEnabled = YES;
[self addSubview:_inputBackgroundView];
[_inputBackgroundView release];
[_inputBackgroundView setImage:[[UIImage imageNamed:#"Footer_BG.png"] stretchableImageWithLeftCapWidth:80 topCapHeight:25]];
// Text field
_textField = [[UITextView alloc] initWithFrame:CGRectMake(70.0f, 0, 185, 0)];
_textField.backgroundColor = [UIColor clearColor];
_textField.delegate = self;
_textField.contentInset = UIEdgeInsetsMake(-4, -2, -4, 0);
_textField.showsVerticalScrollIndicator = NO;
_textField.showsHorizontalScrollIndicator = NO;
_textField.font = [UIFont systemFontOfSize:15.0f];
[_inputBackgroundView addSubview:_textField];
[_textField release];
[self adjustTextInputHeightForText:#""];
Fill UITextView delegate methods:
- (void) textViewDidBeginEditing:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
- (void) textViewDidEndEditing:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
if ([text isEqualToString:#"\n"])
{
[self performSelector:#selector(inputComplete:) withObject:nil afterDelay:.1];
return NO;
}
else if (text.length > 0)
{
[self adjustTextInputHeightForText:[NSString stringWithFormat:#"%#%#", _textField.text, text]];
}
return YES;
}
- (void) textViewDidChange:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
And the trick is...
- (void) adjustTextInputHeightForText:(NSString*)text {
int h1 = [text sizeWithFont:_textField.font].height;
int h2 = [text sizeWithFont:_textField.font constrainedToSize:CGSizeMake(_textField.frame.size.width - 16, 170.0f) lineBreakMode:UILineBreakModeWordWrap].height;
[UIView animateWithDuration:.1f animations:^
{
if (h2 == h1)
{
_inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - _InputBarHeight, self.frame.size.width, _InputBarHeight);
}
else
{
CGSize size = CGSizeMake(_textField.frame.size.width, h2 + 24);
_inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - size.height, self.frame.size.width, size.height);
}
CGRect r = _textField.frame;
r.origin.y = 12;
r.size.height = _inputBackgroundView.frame.size.height - 18;
_textField.frame = r;
} completion:^(BOOL finished)
{
//
}];
}
Facebook has released an open-source package called Three20 that has a multi-line text field. You can use this pretty easily for an expanding text field.
As for the "Done" button, you can set your view controller as a UITextFieldDelegate. Then use this method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Do whatever you want for your done button
return YES;
}
In the case of Three20, use this method of TTTextEditorDelegate:
- (BOOL)textFieldShouldReturn:(TTTextEditor *)textField {
// Do whatever you want for your done button
return YES;
}
Well, I had a similar problem, and what I ended up using is actually create a disabled UITextField as the background and a UITextView above it to get the input... It sucks that iPhone API cannot have this by default. Also note that this does not auto-expand, but you can do this if you want by handling the textViewDidChange:
As for handling the return key, try implementing the following method from the UITextViewDelegate:
- (void)textViewDidChange:(UITextView *)inTextView {
NSString *text = inTextView.text;
if ([text length] > 0 && [text characterAtIndex:[text length] -1] == '\n') {
inTextView.text = [text substringToIndex:[text length] -1]; // remove last return from text view
[inTextView resignFirstResponder]; // hide keyboard
}
}
(void)textEditorDidBeginEditing:(TTTextEditor *)textEditor {
And
(void)textEditorDidEndEditing:(TTTextEditor *)textEditor {
might be what you're looking for. Enjoy!