How to create UIView to show text like in Terminal? - ios

I'm making telnet program and I have everything resolved but the text output.
I want it to have console look and feel, and basic controls like UITextField or UILabel do not work at all for this.
Is there any custom control to do this?
How can I write one myself?

You can use UITextView to display text and UITextField to input the text and override textFieldShouldReturn: method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSString* inputString = textField.text;
[inputString lowercaseString];
// when you type 'clear' clear output view
if ([inputString isEqualToString:#"clear"])
{
// Your text view outlet to display the data
[self.outputTextView clear];
}
else
{
[self.outputTextView setText:inputString concatenate:YES];
// Your text field outlet to input the data
[self.inputTextField setText:#""];
return YES;
}
Remember to set up text field delegate for input text field.

Related

Select specific text in UITextView to fire a segue

I’m trying to add a function to my application where the user can select a PRESPECIFIED text by me in order to fire a segue and get an explanation/information about what the user has selected.
The following text is an example:
Hawaii is the most recent of the 50 "U.S. states" and joined the Union on August 21, 1959.
The user can select "U.S. states" and will get information about U.S. states.
So Can I do that with Xcode or do I have to work with some html editing application? and if so, where would be a good place (online or book) to start learning how to do such a thing?
Thanks.
There may be better ways, but I know of one way to do what you're asking.
Create a NSMutableAttributedString with the text you want shown
Determine the range of characters you want to be clickable and using setAttributes:rangeSet, add the NSLinkAttributeName attribute with a value of a string you can use to identify the clicked region.
Create a UITextView and set the attributedString property to the string from above
Set some object as the delegate of your UITextView
Override the textView:shouldInteractWithURL:inRange: method. Return NO here, but do what you want with the URL, created from the string you used before.
Here's the code part. I'll leave the UITextView creation to you.
-(void)viewDidLoad
{
[super viewDidLoad];
NSMutableAttributedString *attString = self.textView.attributedText.mutableCopy;
[attString setAttributes:#{NSLinkAttributeName:#"123"} range:NSMakeRange(0, 10)];
self.textView.attributedText = attString;
self.textView.delegate = self;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(#"%#", URL);
return NO;
}
You can do it in Xcode. You will want to create a segue in the storyboard and add an identifier to the segue. Then in one of the UITextViewDelegate methods, you can check the text in the text view. If it matches, you can call the segue programmatically using -performSegueWithIdentifier:sender:. Alternatively, you can create the entire segue programmatically using -segueWithIdentifier:source:destination:performHandler:.

Click spacebar on the keyboard programmatically

I have an issue with a resizing UITextView
It resizes correctly when user is typing and resizes incorrectly, when i set its text programmatically
with [textView setText:]
I want to set its text to blank by doing setText:#"" and then clicking the spacebar programmatically
How do i click the space bar programmatically ?
Here are screenshots of my problem
More than likely you've put code in (void)textViewDidChange:(UITextView *)textView that doesn't belong there.
The text view calls this method in response to user-initiated changes
to the text. This method is not called in response to programmatically
initiated changes.
The solution is to create a separate method that runs regardless of whether the text was changed programmatically or by the user. It will probably looks something like this.
- (void)setText:(NSString *)text {
self.myTextView.text = text;
[self updateTextViewSize];
}
- (void)textViewDidChange:(UITextView *)textView {
[self updateTextViewSize];
}
- (void)updateTextViewSize {
//sizing logic goes here
}

Set text inputed from keypad on UILabel on UIButton click

i want to write the text to UILabel which is inputed from iphone's keypad. when i tapped button as in screen shot. how i can do that ? i don't want to use Textfield.
-(IBAction) buttonTapped {
[self.yourlabel setText:[textField text]];
}
Add a UITextField (you can name it hiddenTextField) some where, and set it hidden in view, so it'll become invisible to everyone, in viewDidLoad method of that UIViewController write,
[hiddenTextField becomeFirstResponder]; //it will make UIKeyBoard show on screen
On your UIButton action write,
- (IBAction)myAction {
myLabel.text = hiddenTextField.text;
//Don't forget to set `hiddenTextField` delegate to self.
[hiddenTextField resignFirstResponder];
}

Add dynamic text on uilabel programmatically

-(void)subImagetap:(UITapGestureRecognizer *)subimagetap
{
UIView *myV = (UIView *)subimagetap.view ;
NSArray *lbarray = subimagetap.view.subviews;
for (UIView *textV in lbarray) {
UILabel *textlb = (UILabel *)textV;
if([textlb isKindOfClass:[UILabel class]]) {
NSLog(#"%#", textlb.text);
textlb.text = #"Tapped";
}
}
}
The above mentioned code is my UITapGestureRecognizer code through with I tap on a label and changed his text to "Tapped" but my requirement is when I tap on the label the textpad of iphone will open and i could able to put text in that label.
You should use UITextFields if you want the user to be able to directly manipulate their content. Using a text field, you can still do what you're doing by modifying the text property, but UITextField supports editing with the keyboard as well.
You can even dress the text field up to look just like a UILabel through use of its borderStyle property. (Set it to UITextBorderStyleNone)

UITextField and UIButton

There is a textField and a button in my view, and if the
textfield is empty I do not want to user can click the button.
When user text something in the textfield, the button will bu
clickable.
How can I do this? Thank you.
for your UITextField, you can set:
[textField addTarget:self action:#selector(editing:) forControlEvents:UIControlEventAllEditingEvents];
Then in your editing: have:
-(void)editing:(UITextField *)sender {
myButton.enabled = ![sender.text isEqualToString:#""];
}
Listen for changes to the text field. As the text changes, update the button's enabled property based on whether there is text or not. Of course you also need to set the button's state at the beginning as well.
// Setup the text field change listener (this can be done in IB if appropriate)
// Put this in viewDidLoad if not using IB.
UITextField *myTextField = ... // a reference to the text field
[myTextField addTarget:self action:#selector(textFieldChangedAction:) forControlEvents:UIControlEventEditingChanged];
// Initialize the button's state (put this in viewDidLoad)
myButton.enabled = myTextField.text.length > 0;
// The method called as the text changes in the text field
- (void)textFieldChangedAction:(UITextField *)sender {
myButton.enabled = sender.text.length > 0;
}
If you want, you can "hide" the button by setting the alpha value of the button to 0 and when the textfield is at least one character long, then set the button's alpha value to 1 to "show" the button. I think this conceptually is easy to do and very presentable to the user.
Set the textfield.delegate to self
Then in delegate that is textfielddidbeginediting
(
Check is textfield is #""
Then disable the button
Else
Enable the button
)
And there is one more. Delegate u can use
Textdidchangecharacters
And in viewdidload first state of the button should be disabled

Resources