I have this code for my calculator that lets me delete one number at a time!
- (IBAction)deleteButton:(id)sender {
NSString *string = [Screen text];
int length = [string length];
NSString *temp = [string substringToIndex:length-1];
if ([temp length] == 0) {
temp = #"0";
}
[Screen setText:temp];
}
It works, but whenever I enter another number, it resets back to the whole thing, so lets take this as an example.
I have the number 5678, (I deleted 678), So my new number is 5, (Now if I press another number), it goes back to 56781 ( 1 being the new number)
Heres my full code for my project! ---> http://txt.do/oduh
As seen in your code, you are using SelectNumber to store value everywhere, but in deleteButton method you are not storing new value in SelectNumber. So you need to set the new value in deleteButton.
- (IBAction)deleteButton:(id)sender {
NSString *string = [Screen text];
int length = [string length];
NSString *temp = [string substringToIndex:length-1];
if ([temp length] == 0) {
temp = #"0";
}
SelectNumber = [temp intValue]; // set new value here
[Screen setText:temp];
}
Related
I am using the below written method for getting the current selected line number of textView having attributed text. It works fine if last line is not blank. If last line goes blank this method is not giving a proper line number.
Method :
- (NSInteger)getCurrentLineIndex{
/*
Get Current Line Inside UITextView
*/
CGPoint cursorPosition = [self caretRectForPosition:self.selectedTextRange.start].origin;
return (NSInteger)(cursorPosition.y / self.font.lineHeight);
}
Is there any other way to get the current line index?
Fixed : I already had mRangesArray in which each object gave a range of each line. Using those range objects and comparing with the selected range, I was able to fix the problem.
1) Get Ranges Array
- (void)analyse
{
if (self.mRangesArray != nil) {
[self.mRangesArray removeAllObjects];
}
NSString *string = self.text;
NSUInteger numberOfLines, index, stringLength = [string length];
NSMutableArray *ranges = [NSMutableArray new];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
{
NSRange range = [string lineRangeForRange:NSMakeRange(index, 0)];
[ranges addObject:[NSValue valueWithRange:range]];
}
if (self.mRangesArray == nil) {
self.mRangesArray = [[NSMutableArray alloc]init];
}
self.mRangesArray = ranges;
}
2) Compare Range Objects inside the RangesArray with the current selected Range
- (NSInteger)getCurrentLineIndex{
/*
This method will be used to get mRangesArray
*/
[self analyse];
/*
Get Current Line Inside UITextView
*/
NSRange selectedRange = self.selectedRange;
__block NSInteger index = [self.mRangesArray count]-1;
[self.mRangesArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSRange objectRange = [obj rangeValue];
if (*stop == NO) {
if (NSLocationInRange(selectedRange.location, objectRange)) {
index = idx;
*stop =YES;
return;
}
}
}];
}
Thanks guys for your help. Cheers :)
EDIT: I've obviously ignored the currently selected part of your question.
I'll still keep the code here in case someone needs it.
Have you tried counting the newline separator?
let str = "First\nSecond\nThird"
let tok = str.componentsSeparatedByString("\n")
print(tok.count) // Hopefully prints "3"
Try this code it works
UIFont * fontNAme = [UIFont boldSystemFontOfSize:13.0];
CGSize size = [textView.text fontNAme constrainedToSize:textView.frame.size lineBreakMode:UILineBreakModeWordWrap];
int lineNumber = size.height / fontNAme .lineHeight;
NSLog(#"line = %d", lineNumber);
I am trying to get single characters from NSString, like "ઐતિહાસિક","પ્રકાશન","ક્રોધ". I want output like 1)ઐ,તિ,હા,સિ,ક 2) પ્ર,કા,શ,ન 3) ક્રો,ધ, but output is coming like this 1)ઐ , ત , િ , હ , િ , ક 2) પ , ્ , ર , ક , ા , શ , ન 3)ક , ્ , ર , ો , ધ
I have used code like below:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<strElement.length; i++)
{
NSString *str = [strElement substringWithRange:NSMakeRange(i, 1)];
[array addObject:str];
}
NSLog(#"%#",array);
Let's take strElement as "ક્રોધ" then I got output like this ક , ્ , ર , ો , ધ
But I need output like this ક્રો,ધ
Is there any way that I can get the desired output? Any method available directly in iOS or need to create it by my self then any way or idea how to create it?
Any help is appreciated
Your code is assuming that each character in the string is a single unichar value. But it is not. Some of the Unicode characters are composed of multiple unichar values.
The solution is to use rangeOfComposedCharacterSequenceAtIndex: instead of substringWithRange: with a fixed range length of 1.
NSString *strElement = #"ઐતિહાસિક પ્રકાશન ક્રોધ";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSInteger i = 0;
while (i < strElement.length) {
NSRange range = [strElement rangeOfComposedCharacterSequenceAtIndex:i];
NSString *str = [strElement substringWithRange:range];
[array addObject:str];
i = range.location + range.length;
}
// Log the results. Build the results into a mutable string to avoid
// the ugly Unicode escapes shown by simply logging the array.
NSMutableString *res = [NSMutableString string];
for (NSString *str in array) {
if (res.length) {
[res appendString:#", "];
}
[res appendString:str];
}
NSLog(#"Results: %#", res);
This outputs:
Results: ઐ, તિ, હા, સિ, ક, , પ્ર, કા, શ, ન, , ક્રો, ધ
I am fairly new to Objective C and am attempting to develop an app using Xcode5.
I am storing strings (either composed of numbers 1-9 or N/A) in a NSMutableDictionary.
When users get to the "Review your inputed results page" I want them to be able to manually go into a text field, delete the value present and retype their new value if necessary. However, I don't know how to reload this information into the system so that the new values will carry over into the email client, which basically sends the results to whatever email address the user wishes.
Currently, the values are being loaded using ViewDidAppear upon entering the UIView, but I think I need to call it again if, for example, textField1 is updated.
I have methods for all the textFields that are textField(insert correct number here)IsUpdated and inside those I want to store the new value to the NSMutableDictionary (which I believe I can already do).
The issue is I cannot figure out how to get the current version of the dictionary that was loaded upon entering the UIView to update so that the information in ViewDidAppear updates for the email.
Hope that made sense.
As I said, definitely new to Objective C.
Below is the viewDidAppear method.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
AppDelegate *app = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSMutableDictionary *results = [app results];
NSString *firstResult = [results valueForKey:#"first"];
NSString *secondResult = [results valueForKey:#"second"];
NSString *thirdResult = [results valueForKey:#"third"];
if ([firstResult isEqual: #"N/A"]) {
self.Result1.text = results[#"first"];
} else {
int firstResultInt = [firstResult intValue]; firstResultInt++;
[_Result1 setText:[NSString stringWithFormat:#"%i", firstResultInt]];
}
if ([secondResult isEqual: #"N/A"]) {
self.Result2.text = results[#"second"];
} else {
int secondResultInt = [secondResult intValue]; secondResultInt++;
[_Result2 setText:[NSString stringWithFormat:#"%i", secondResultInt]];
}
if ([thirdResult isEqual: #"N/A"]) {
self.Result3.text = results[#"third"];
} else {
int thirdResultInt = [thirdResult intValue]; thirdResultInt++;
[_Result3 setText:[NSString stringWithFormat:#"%i", thirdResultInt]];
}
self.diningResult.text = results[#"dining"];
self.basementResult.text = results[#"basement"];
self.atticResult.text = results[#"attic"];
self.carResult.text = results[#"car"];
self.hallwayResult.text = results[#"hallway"];
self.garageResult.text = results[#"garage"];
self.other1Result.text = results[#"other"];
self.other2Result.text = results[#"other1"];
self.other1Name.text = results[#"other1name"];
self.other2Name.text = results[#"other2name"];
NSMutableString * str = [NSMutableString new];
[str appendString:#"Bedroom: "];
if ([firstResult isEqual: #"N/A"]) {
[str appendString: firstResult];
} else {
int firstResultInt = [firstResult intValue]; firstResultInt++;
NSString *firstResultString = [NSString stringWithFormat:#"%d",firstResultInt];
[str appendString: firstResultString];
}
[str appendString:#"\n"];
[str appendString:#"Living Room: "];
if ([secondResult isEqual: #"N/A"]) {
[str appendString: secondResult];
} else {
int secondResultInt = [secondResult intValue]; secondResultInt++;
NSString *secondResultString = [NSString stringWithFormat:#"%d",secondResultInt];
[str appendString: secondResultString];
}
[str appendString:#"\n"];
[str appendString:#"Kitchen: "];
if ([thirdResult isEqual: #"N/A"]) {
[str appendString: thirdResult];
} else {
int thirdResultInt = [thirdResult intValue]; thirdResultInt++;
NSString *thirdResultString = [NSString stringWithFormat:#"%d",thirdResultInt];
[str appendString: thirdResultString];
}
[str appendString:#"\n"];
[str appendString:#"Dining:"];
[str appendString:self.diningResult.text];
[str appendString:#"\n"];
//Code goes on to do the same with all other fields. all strings led by "str" get transferred over to the email
self.emailString = [NSString stringWithString:str];
}
The code should not call viewDidAppear, it's the responsibility of the framework to call viewDidAppear at the appropriate times. Instead, you should make a separate methods, e.g. UpdateMailContents and UpdateTextFields. Then call those methods from viewDidAppear, and call UpdateMailContents from the textFieldDidEndEditing method of the UITextFieldDelegate protocol.
I used a string array for emoticons like this:
NSArray *emoticons = #[#"[smile]",#"[cry]",#"[happy]" ...]
then in a UITextView displaying a string like this:
I'm so happy now [happy] now [smile]
When I click a backward or delete button, if the last word is in emoticons, I want a whole emoticon string be deleted, not the last one character only.
Any idea?
Try this,
NSString *string = self.textView.text;
__block NSString *deleteWord = nil;
__block NSRange rangeOfWord;
[string enumerateSubstringsInRange:NSMakeRange(0, self.textView.selectedRange.location + self.textView.selectedRange.length) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
deleteWord = substring;
rangeOfWord = enclosingRange;
*stop = YES;
}];
if ([emoticons containsObject:deleteWord]) {
string = [string stringByReplacingCharactersInRange:rangeOfWord withString:#""];
self.textView.text = string;
self.textView.selectedRange = NSMakeRange(rangeOfWord.location, 0);
}
You might achieve something like this with the UITextViewDelegate method textView:shouldChangeTextInRange:replacementText: checking what is about to be deleted and remove the whole [emoticon] word.
I am giving you the idea that i used.
as you do not mentioned what you used as emoticons.
but for delete logic i think you will get idea from my this code.
if ([string isEqualToString:#""]) {
NSString *lastChar = [txthiddenTextField.text substringFromIndex: [txthiddenTextField.text length] - 1];
NSLog(#"Last char:%#",lastChar);
txthiddenTextField.text = [txthiddenTextField.text substringToIndex:[txthiddenTextField.text length] - 1];
NSString *strPlaceHolder;
strPlaceHolder = txthiddenTextField.text;
if([lastChar isEqualToString:#"]"])
{
int j = 1;
for (int i = [txthiddenTextField.text length]-1; i >=0; --i)
{
NSString *lastChar = [txthiddenTextField.text substringFromIndex: [txthiddenTextField.text length] - 1];
if([lastChar isEqualToString:#"["])
{
NSLog(#"%d",j);
txthiddenTextField.text = [txthiddenTextField.text substringToIndex:[txthiddenTextField.text length] - 1];
// NSLog(#"Processing character %#",strPlaceHolder);
break;
}
txthiddenTextField.text = [txthiddenTextField.text substringToIndex:[txthiddenTextField.text length] - 1];
j = j+1;
}
}
NSLog(#"My text fild value :%#",txthiddenTextField.text);
return YES;
}
So, from here you have to check if the closing bracket is coming or not.
if closing bracket will come then up to opening bracket you have to delete.
then whole emoticon will delete.
hope this helps....
I've set up a simple if statement that says if the input length is equal to 3 make the next character a "-".
It works well, but I'd like the "-" to automatically be put there after I press my button for the 3rd time. So I press, "1", "2", then when I press the "3" it automatically puts a "-" directly afterwards. Currently the "-" only gets placed when I hit the button for the 4th time?
-(IBAction)buttonDigitPressed:(id)sender {
NSString *val = phoneNumberLabel.text;
int length = [val length];
} else {
NSString *tagValue = [NSString stringWithFormat:#"%d", [sender tag]];
phoneNumberLabel.text = [val stringByAppendingString: tagValue];
if (length == 3) {
phoneNumberLabel.text = [val stringByAppendingString:#"-"];
}
if (length == 7) {
phoneNumberLabel.text = [val stringByAppendingString:#"-"];
}
}
}
Any help would be appreciated, greatly! Thanks!
-(IBAction)buttonDigitPressed:(id)sender {
NSString *val = phoneNumberLabel.text;
NSString *newValue = #"";
NSString *dash = #"";
int length = [val length];
if ( ((length == 3) || (length == 7)) ) {
dash = #"-";
}
newValue = [NSString stringWithFormat:#"%#%#%d", val, dash, [sender tag]];
phoneNumberLabel.text = newValue;
}
Add the following code in buttonClicked method
if ([myLabel.text length] == 3)
{
myLabel.text = [val stringByAppendingString:#"-"];
}
Just add that code in the same method after you update the label with the integer.
Try this method,hope it will help you.
Keep UILabel object blank in xib.
- (IBAction)pressAction:(UIButton*)sender
{
if (lbl.text.length<=2) {
lbl.text=[lbl.text stringByAppendingFormat:#"%i",sender.tag];
}
if (lbl.text.length>=3){
lbl.text=[lbl.text stringByAppendingString:#"-"];
}
}
You can send the button the events. like [button sendActionsForControlEvents:UIControlEventTouchUpInside]
Or even perform selector after delay may do the trick for you.
both the cases you need not press a button
For the addition of "-" just check how many characters have been entered and when the third character has been entered add the "-".