views:

610

answers:

4
+1  Q: 

iPhone Text Field

Hello, I have run into an issue that I have yet to be able to solve. Hopefully someone can help. I have an app that I am using to do simple calculations, some of the results are larger than the UItextField. Does anyone know how to limit the output to say 10 characters like say a calculator would using exponents. This is the code I am using currently.

myVar.text = [[NSString alloc]initWithFormat:@"%2.1", myVar]

This just limits the decimal place. The 2 in front of the decimal does nothing.

+1  A: 

You could do it this way. Hope this helps

#define MAX_LENGTH 20

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0)
    {
        return NO; // return NO to not change text
    }
    else
    {return YES;}
}

ref: http://stackoverflow.com/questions/433337/iphone-sdk-set-max-character-length-textfield

Zteeth
This is usefull code. It only limits input from key pad though.
New to iPhone
Why a -1 for me??
Zteeth
A: 

Check here. Sounds like you want 1.2E or _1.2_e.

EightyEight
This write all output in scientific notation. Not all needs to appear that way. Only if over a certain length.
New to iPhone
Check the length of the output then, and only convert if it's longer than your preference.
EightyEight
A: 

You may be able to find the answer to your question here.

titaniumdecoy
A: 

Any other suggestions. This is really troubling me.

New to iPhone