how to check if a text label is greater than another text label in Xcode - ios

I am relatively new to xCode and I have 2 integer text labels Text1 and Text2 text.
I'm looking for some code that would compare if Text1.text is greater than Text2.text and then if another text field Text3.text would be equal to the value of Text1.text.
Appreciate any help.

In the attributes inspector I assume. Select the text, go to attributes inspector (top right corner at the utility area, forth in a row. Looks like a square head with ears) and check the size.
Well, if you want xcode to check the size, you'll need to make an if or switch statement. Use enums like .fontSize

The text fields are just views that display a string.
You can access the string that they contain via the text property:
NSString *text1 = _text1.text;
Now you have the string that is displayed in the textview ( "34" for example)
You can use the NSString method intValue to turn this string into an integer:
int text1Value = [text1 intValue];
You can now compare the value of this to another integer:
if (text1Value > 42) {
_textLargest.text = [NSString stringWithFormat:"%d", textValue1];
}
Main points:
Text fields are just views; you need to work with the data they are displaying rather than treat them as objects
You need to use the correct type for your comparison. The text field contains text but you need to convert it to a number to make it meaningful
You need to convert the number back into a string if you want it to appear in the text field's text property [NSString stringWithFormat:"%d", textValue1]

You can take and compare the int values as follows
if([Text1.text intValue] > [Text2.text intValue] )
{
Text3.text=[NSString stringWithFormat:"%#", Text1.text];
}

Related

iOS - Secure Text Entry Causes Font Size And Style Of Last Character To Be Different

By default, secureTextEntry is set to YES. When the text is entered in the text field, ••••••• will be displayed. But this time. when secureTextEntry is set to NO, the font size and style of the last character of the string will always be different. Plus, there will be a big spacing between the cursor and the whole string. See screenshot below.
I found an answer from this site. http://www.jianshu.com/p/72271c023d6d
After changing the secureTextEntry property of UITextField. Just replace the string with and empty string #" ", then copy the original string back again.
Here's the code:
- (IBAction)secureSwitchAction:(id)sender
{
self.passwordTextField.secureTextEntry = !self.passwordTextField.secureTextEntry;
NSString* text = self.passwordTextField.text;
self.passwordTextField.text = #" ";
self.passwordTextField.text = text;
}

Why are the ranges for my formatted text shifting in iOS 7?

My app includes a text formatting tool that offers buttons for things like bold, italic and color and shows the formatted text by generating an NSAttributedString and setting that to the attributedText property of a UITextView. After the user selects text and taps a button, I get the selectedRange property of the UITextView, then get the current attributedText property of the UITextView, add another attribute to the text based on the selected range, and then assign it back to the attributedText property of the UITextView again.
Starting with iOS 7, my text formatting started displaying at the wrong location in the text, usually shifted a couple characters forward. After some testing I noticed that this only happened after an empty line (e.g., a paragraph of text with two line breaks after it) and the formatting was offset by one character for each empty line proceeding it.
After more testing I found that when I set the attributedText property for the first time, any sequence of two line breaks is changed to a line break, then a "line separator" character (Unicode 8232) and then the second line break. The new characters are definitely added by the attributedText assignment, as I can see from outputting the integer value of each character immediately before and immediately after that action. However, the selectedRange property of the UITextView ignores the line separator characters, so any range that it returns is now incorrect.
I've already found a workaround, which I'll add as an answer in a moment. I'm mainly posting this in case anyone else is having problems with it. Also, I've reported this to Apple as bug 15349335.
I wrote this method to adjust ranges returned by the selectedRange property to account for these extra line separator characters:
- (NSRange)adjustRangeForEmptyLines:(NSRange)range inText:(NSAttributedString *)text byChars:(int)chars {
int emptyLinesBeforeRange = 0;
int emptyLinesWithinRange = 0;
for (int i=0; i<(range.location + range.length); i++) {
int thisCharacter = [text.string characterAtIndex:i];
//NSLog(#"thisCharacter: %i", thisCharacter);
if (thisCharacter == 8232) {
if (i < range.location) {
emptyLinesBeforeRange++;
} else {
emptyLinesWithinRange++;
}
}
}
//NSLog(#"found %i + %i empty lines", emptyLinesBeforeRange, emptyLinesWithinRange);
range.location += (emptyLinesBeforeRange * chars);
range.length += (emptyLinesWithinRange * chars);
return range;
}
I can set the byChars argument to 1 or -1 depending on which way I want to adjust. This has been working for me for a few weeks now, but if anyone has an alternate solution, I'd be curious to see it.

Split a string with '\n', insert it into UITableViewCell and get height

I'm developing an iOS 4 application with latest SDK and XCode 4.2.
I use a web service to retrieve some text data. I don't know its length so I will need to split that strings to fit inside a custom UITableViewCell.
This custom UITableViewCell will have an UILabel. And this label will be filled up with the text retrieved from web service.
The text will be country names: Spain, France, USA, Italy, etc. I will process this names to append them to a unique string. I will do it this way: "Spain - France - USA - ...".
One of my problems was that this line could be so long, and I need to split it into lines to fits UILabel width. I have solve this problem, checking every time I add a ' - ' if the string will get bigger than UILabel width. So, I will have a string like this: "Spain - France\nUSA - ...".
Ok. Now, I have a string with \n that will fit inside UILabel. So, I will need to modify UITableViewCell height to fits with UILabel height.
But, when I use [NSString sizeWithFont:[UIFont systemFontOfSize:kFontSize]]; I'm not getting the real height, I'm always getting 21.0f.
I've found this tutorial about UITableView Dynamic Height, but I don't know how to use it with my code.
In a nutshell, I need to append ' - ' character to country names, but it there is a return carried (because the string will be rendered in another line by UILabel), I don't have to append ' - '. And, when I get this string, I will have to resize custom UITableViewCell to UILabel height.
Any clue?
Is that something you are looking for?
//create a CGFloat variable
CGFloat _height = 0;
//find out the size for your text. Instead of 255 insert the width of your label
CGSize _textSize = [yourString sizeWithFont:[UIFont systemFontOfSize:kFontSize] constrainedToSize:(CGSize) { 255, 9999 }];
//add the height of that CGSize variable to your height in case you will need to add more values
_height += _textSize.height;
//eventually some other calculations
Hope it helps

UITextView increment character to the left

I have a UITextView that has a fixed width and height. I pre-populate the entire textfield with blanks.
I would like to insert a character with the push of a button that will erase the last blank character, insert my string character and then place the cursor at the beginning of the newly inserted string. I am trying to achieve inserting special fonts right to left and bottom to top.
It is working with the first button push and on the second button push the new value is inserted in the correct position to the left, however, the cursor will not move to the left after the second button push, it remains to the right after the second string insert.
Here is my code...
-(IBAction)chartP:(id)sender {
NSRange currentRange = myChart.selectedRange;
if (currentRange.length == 0) {
currentRange.location--;
currentRange.length++;
}
myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange
withString:[NSString string]];
currentRange.length = 0;
myChart.selectedRange = currentRange;
myChart.text = [myChart.text stringByAppendingString:#"p"];
myChart.selectedRange = NSMakeRange(myChart.selectedRange.location -1, 0);
}
Can someone assist me with what I am missing here to continually increment to the left with my string inserts?
How about flipping the text area:
myChart.transform = CGAffineTransformMakeScale(-1,-1);
It sounds like you are trying to implement right-to-left text direction by faking it. Why not do the real thing?
Here's a question that covers the topic:
Change the UITextView Text Direction
If you need bottom-to-top entry, and you have the ability to use a custom font, perhaps you can apply a transformation to the UITextView and y-flip it. Look at the transform property of UIView. (Things like the text selection loupe may break, but it's worth a try.)

how to make a Text prevent the inputs more than 'x' number of digits?

My question is:
I want the Text field in Eclipse RCP to allow only the digits to be keyed in as inputs and the requirement is that it should allow only up to 'x' (let's say x=5) characters.
How can I accomplish it in RCP ?
I've tried the code like:
txtInput.addListener(SWT.Verify, new DecimalText(
txtInsuranceValue, 8, 1000.00));
where txtInputis a Text field. But this listener failed when I made repeated input into this field at run time.
Any alternatives please ?
This will set the maximum number of characters in your Text control to five and allow only digits to be entered:
txtInput.setTextLimit(5);
txtInput.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent event) {
// Assume we allow it
event.doit = true;
String text = event.text;
char[] chars = text.toCharArray();
//Don't allow if text contains non-digit characters
for (int i = 0; i < chars.length; i++) {
if (!Character.isDigit(chars[i])) {
event.doit = false;
break;
}
}
}
});
EDIT: I've modified my answer to allow a user to also set text with "setText(String string)" while still disallowing non-digit characters.
In general, solutions like adding verifyListeners/focusListeners are not complete and won't work with all situations. For example, what happens when the user copy-pastes some text into this text box?
If all you want is to get numerical inputs from the user, you should be using Spinner widget instead of Text widget. You could easily set the minimum and maximum limits on the Spinner.
See the snippets for more usage.

Resources