Thanks in advance for your help. I try to boost my learning curve of Objective-c and defy myself with a lot of cases.
I try to do a simple app which simulates the comportment of a terminal session:
First step: a prompt is waiting and I enter a first command: eg. date. Then I get a result. Second: a prompt is waiting again below the result. Then I give a second command: time
etc.
I did a lot of tests with an UItextField to input different texts and commands, and a UITextView to display the results. I also use an NSMutable Array to stock all inputs/results. Nothing work very well. I would like to get your advice on that matter and that you point me the best approach or a code source to learn to reproduce a terminal gui. Is an array a good solution, how to place the textField at the end of the textView, etc.? Thanks+
This is just a general approach of what you want to achieve.
Use a single UITextView for input and output.
At first, add a simple character to your UITextView, for example ">", so the user starts typing after this character.
Implement this UITextView delegate method to listen on when the user taps "return" :
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
// Handle what happens when user presses return
return NO;
}
return YES;
}
What I would do here is when the user presses return, get the whole UITextField's content, and use something like NSArray *stringArray = [_textField.text componentsSeparatedByString: #">"];. That way, the last element of your array is the last command the user entered. Then, test the command and append the appropriate answer to the UITextView. Don't forget to add #"\n>" after it so you prompt the user a new command line.
What's left to do here is prevent the user from erasing your ">".
It's an idea, there's probably many other ways to do it. Comment if you need more details on something !
SPOILER ALERT : full code
In my storyboard, I simply have a UITextView linked to ViewController.h, with the name textView. Note that the following code does not handle the user removing text from the UITextView. You can test the code by typing "hello" in the console.
ViewController.m :
#import "ViewController.h"
#interface ViewController () {
// Store supported commands and outputs
NSDictionary *commands;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize textView
_textView.text = #">";
[_textView becomeFirstResponder];
// Init supported commands with associated output
commands = [NSDictionary dictionaryWithObjectsAndKeys:#"Hello World !", #"hello", nil];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// Deleting something
if([text isEqualToString:#""]) {
UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition *end = [textView positionFromPosition:start offset:range.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
NSString *textToReplace = [textView textInRange:textRange];
NSLog(#"%#", textToReplace);
if ([textToReplace isEqualToString:#">"]) return NO;
return YES;
}
if([text isEqualToString:#"\n"]) {
// Handle what happens when user presses return
NSArray *stringArray = [_textView.text componentsSeparatedByString: #">"];
NSLog(#"Last command : %#", [stringArray lastObject]);
[self handleCommand:[stringArray lastObject]];
return NO;
}
return YES;
}
-(void)handleCommand:(NSString*)command {
NSString *output = [commands objectForKey:command];
// If an unsupported command was typed
if (output == nil) {
output = #"Unknown command";
}
// Write output to the textView
_textView.text = [_textView.text stringByAppendingString:#"\n"];
_textView.text = [_textView.text stringByAppendingString:output];
_textView.text = [_textView.text stringByAppendingString:#"\n>"];
}
This is the textView's content from the simulator :
>hello
Hello World !
>yes
Unknown command
>
Unknown command
>hello
Hello World !
Many ways to do this. If I were doing it I think I would do the following:
A dictionary to hold the input and output for each command
Very large UITextView with all entries in the dictionary outputted in the format you like
A no border UITextField to act as the prompt.
You would have to write the following:
A method to place the UITextFiled at the right line of the UITextField
A method to populate the Diciotnary
A method to populate the UITextField from the dictionary
Related
I want to set a range i.e between $350000 & $800000 for a text field. If the entered number is less than this range then an alert message should pop up. Plz help
In Objective-C using NSNumberFormatter you can use like this:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.minimum = #10;
nf.maximum = #20;
NSNumber *a = [nf numberFromString:#"12"];
NSNumber *b = [nf numberFromString:#"22"];
//if the number is beyond the range, it will return nil, so in above b is nil
//so the following check will show the log and alert
if (!a || !b) {
NSLog(#"Either of them is out of range");
//show the UIAlert here
}
EDIT:
You should call the above in following scenarios:
User enter a value and does some action. In the beginning of the action use the above.
Or every time the user enters and moves away from the textfield, then call the above in textFieldDidEndEditing: delegate method of UITextField.
Note: Since your number has $ prefixed, please make sure you set the currency style in nf as well, instead of trimming it.
Use - (void) textViewDidEndEditing:(UITextView *)textView method to check the intergerValue of text and if it doesn't match your requirement show the popup.
If wanna check it on the go use - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
NSString *finalText = [textView.text stringByAppendingPathComponent:text];
finalText = [finalText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//enable button
if ([finalText integerValue] < 350000 || [finalText integerValue] > 850000) //do what you wanna do
Make sure $ is out of textfield or you remove it before using integerValue.
This can be helpfull
-(void)viewDidLoad
{
[txt addTarget:self action:#selector(CheckingMethod:) forControlEvents:UIControlEventValueChanged];
}
-(IBAction)CheckingMethod:(id)sender
{
if(txt.text.length>9) {
//your popupview
}
else{
}
}
When using a UITextView to gather user input in Objective-C, how can I limit the user from trying to do more than one line break at a time?
So, this would be fine:
This is my text.
Here is some more text.
But this would not be fine:
This is my text.
Here is some more text way down here.
In your ViewController.h add UITextViewDelegate:
YourViewController : UIViewController <UITextViewDelegate>
and in the ViewController.m implement the method textView:shouldChangeTextInRange:replacementText: this way:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
NSMutableString *futureString = [textView.text mutableCopy];
[futureString insertString:text atIndex:range.location];
NSRange rangeOflineBreaks = [futureString rangeOfString:#"\n\n\n"];
if (rangeOflineBreaks.location != NSNotFound) {
return NO;
}
}
return YES;
}
This will be executed every time before the user wants to add some text to the textView and wont let him add another line break if it notices that he wants to add a line break and it finds, after trying adding it (that's why the futureString name) a triple line break mode. In that case he wont let the user add another line break.
Try it out, it should work :)
PS: Don't forget to set your textView delegate the viewController in the viewDidLoad (yourTextView.delegate = self)
I have a UITextView. I have the delegate for myTextView set to self and, when I do normal editing, this method calls just fine:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(#"Called");
}
In my app, I call in my code: [myTextView insertText:#"Hello World"];. When I do, I need to call textView:shouldChangeTextInRange:replacementText: after the text is inserted. How do I do this?
Call it explicitly. Call it before editing and only perform the edit if it returns YES. To call it explicitly you need to know the selected range (get the selected range with selectedTextRange), that's it. You already have the text to add and the text view.
Thanks for the answer, #Wain!
Here's what worked:
- (void)insertText
{
NSString *stringToAdd = #"Hello World";
NSString *replacementText = [myTextView.text stringByAppendingString:stringToAdd];
[napkinTextView insertText:stringToAdd];
[self textView:myTextView shouldChangeTextInRange:NSMakeRange(0, stringToAdd.length) replacementText:replacementText];
}
I have a UITextView in my app which is used to display some string with numbers. These numbers can be phone numbers or other numbers specific to the app. If user taps the phone number they should be asked if they want to make a call (This is done by default). However, if the user taps on the other number which is not a phone number but is specific to the app, the action should be custom e.g. it should call a method in the view controller with the number as an argument.
I had a quick search but couldn't find any easy solution.
Any idea how this could be done? Any help would be appreciated.
I was able to solve this by implementing 'UITextViewDelegate'.
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
DLog(#"URL is %#", URL.absoluteString)
NSString *host = [URL host];
NSNumber *adIdInLink = nil;
NSRange range = NSMakeRange(0, 3);
NSString *subString = [URL.absoluteString substringWithRange:range];
if ([subString isEqualToString:#"tel"] && !host) {
// this is a number - do whatever you want
NSString *stringWithAdId = [URL.absoluteString substringFromIndex:4];
if ([HJUtilities isValidAdId:stringWithAdId]) {
// this is the ad Id a custom number used in my app
adIdInLink = [NSNumber numberWithInteger:[stringWithAdId integerValue]];
}
else {
// this was a phone number - let the default behaviour
return YES;
}
}
}
Add a UITapGestureRecognizer to the UITextView with its delegate set to the parent UIViewController:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTap:)];
[myTextView addGestureRecognizer:recognizer];
The add a handler method to your UIViewController:
- (void)handleTap:(UITapGestureRecognizer *)sender{
UITextView *textView = (UITextView*)sender;
NSString *number = textView.text;
}
eta: You need to add UIGestureRecognizerDelegate as a protocol to your UIViewController too:
#interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
update:
How about using the UITextViewDelegate method textViewDidChangeSelection instead?
Inspect the selectedRange property of the textview to check its contents.
This is my First question on ios
I am using two UITextView objects (textView1 and textView2) in a View, Each of them has some character limit with some following Scenario:
Initially user can only enter into textView1.
When the entered character limit of textView1 is over, the cursor will automatically shift to textView2.
After building the project, If user tap the textView2 and try to write into it, Cursor must shifted to textView1 (because it is empty).
I wrote the code and everything works fine except the third scenario, User can only enter into textView1 but focus is still on textView2
Steps to reproduce:
Build the project
user tap the textView2 first and try to write something.
According to written code, Focus remain in textView2 but user are writing into textView1 (see the attachment)
Here is the snapshot:
Here is the written code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.textView1 becomeFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSInteger restrictedLengthForTextView2 = 130;
NSString *temp=textView.text;
if(textView == self.textView1){
if([[textView text] length] > restrictedLengthForTextView1){
textView.text=[temp substringToIndex:[temp length]-1];
[textView resignFirstResponder];
[self.textView2 becomeFirstResponder];
}
}else{
if([[textView text] length] > restrictedLengthForTextView2){
textView.text=[temp substringToIndex:[temp length]-1];
[self.textView2 resignFirstResponder];
}
}}
- void()textViewDidBeginEditing:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSLog(#"dalknwdlakwd");
if([[self.textView1 text] length] < restrictedLengthForTextView1){
if(textView == self.textView2){
[self.textView2 resignFirstResponder];
[self.textView1 becomeFirstResponder];
}
}}
Please help me here..
please do as per following:
in .h file
#interface ViewController : UIViewController<UITextViewDelegate>
{
IBOutlet UITextView *txtView1;
IBOutlet UITextView *txtView2;
}
#end
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[txtView1 becomeFirstResponder];
txtView2.editable=NO;
}
implement the textView delegate method like below:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(textView.tag==1)
{
if([textView.text length]>25)
{
txtView2.editable=YES;
[txtView2 becomeFirstResponder];
}
}
return YES;
}
i have taken text length limit in first textview as 24 characters as an example.
i hope this will help you.
It is a known bug, with resigning and becoming first responder within the same runloop. Try the following
[textView2 resignFirstResponder];
[textView1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
An update to Annadurai's answer: it's still a bug in 2016! After trying others' suggestions to deselect the surrounding tableview cell, and/or change the tint color only this answer worked for me:
[textView1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
FYI My case was that I was resigning a textView, reloading the table, and returning to the same textView. Before this fix - no cursor on the reloaded textView (Yes I could type and see characters.) After the performSelector... afterDelay - the cursor was visible again.