Select specific text in UITextView to fire a segue - ios

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:.

Related

NSAttributedString with a URL not working

In a swift iOS app, I have set a UILabel with an NSAttributedString on it.
The AttributedString contains a URL set with
[NSLinkAttributeName:NSURL(string:urlStr)!].
All appears fine, meaning that the URL is clearly set as such, but when I tap it nothing happens.
Is there something I should do to make the URL work as one would expect (i.e fire Safari)?
I tried the following but it had no effect at all.
theLabel.userInteractionEnabled = true
NSLinkAttribute is not working with UILabel as the handler method will respond to UItextViewDelegate Only.
You can either use. TTTAttributedLabel or can go with Replacing your UILabel with UITextView. After replacing the same use
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
And don't forget to check for Detection and Behaviour property.
Try this code :
var str: NSMutableAttributedString = NSMutableAttributedString(string: "Google")
str.addAttribute(NSLinkAttributeName, value: "http://www.google.com", range: NSMakeRange(0, str.length))
yourLabel.attributedText = str
This is not directly about the question but just to clarify, UITextField and UILabel does not support opening URLs. If you want to use UILabel with links you can check TTTAttributedLabel.
Also you should set dataDetectorTypes value of your UITextView to UIDataDetectorTypeLink or UIDataDetectorTypeAll to open URLs when clicked. Or you can use delegate method as suggested in the comments.

How to create UIView to show text like in Terminal?

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.

one TextField to 6 others

Quick Question, I hope,
I have a UITextFeild that I want to type in an number and have that number populate into 6 other UITextfields on the same VC.
The first textfiled is called percentage goal while the others are named endmonth1year1percentage, endmonth2year1percentage, endmonth3year1percentage, etc.
I am currently using iOS6 with storey board.
Any help is appreciated. Thanks.
Detect the change in the first text field: UITextField text change event
And then update the text property of other text fields you want to be populated.
try like this,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//replace your textfield names here
textField1.text=textField.text;
textField2.text=textField.text;
textField3.text=textField.text;
textField4.text=textField.text;
textField5.text=textField.text;
return YES;
}
So for the first UITextField you must set the delegate the class that is responsible with the display of the text fields (a view controller or a custom view). For the other UITextFileds you should set a tag like 1,2,3..for each UITextFiled (because you say that will be lots of UITextFields)
After you set the delegate for the first UITextField and setup the tags, you can implement two different delegate methods (depending on what you want).
The method that Sunny provided, this is for instant changes:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
return YES;
}
Second, you can use the following if you want to update the textfields only after the user finished typing and the keyboard is hidden:
-(void)textFieldDidEndEditing:(UITextField *)textField {
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
}
EDIT
So first of all delegate is a pattern heavy used by iOS here is a tutorial that will explain the basic concept of delegate.
Second, some of the UI controls that iOS provide have a delegate instance (after you read the above tutorial you will understand why and how it's working). A class can be a delegate of a custom UI control only if the class implements the required methods that the delegate provides (NOTE: there are optional methods also in the delegate), if the class doesn't implement the required methods a build warning will be shown at the line where the delegate is set.
Third, the method used in this answer are delegate methods of the UITextFiled (check apple docs)
I lost count, [tag][3] is a property available for all UIViews of subclasses of UIView that can be used to identify an object, but be careful that this property by default is 0 so make sure when you set the tag property you will use a value > 0.
For more details please use google and Apple Docs

UITextview doesn't enter editing on touch

I'm having trouble getting my UITextView to become editable when it is touched.
I've included the following code per the Apple documentation.
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(#"begin editing");
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(#"end editing");
return NO;
}
I"ve also read through the apple documentation and have made the textview the delegate, but it still doesn't seem to be working. I'm pretty much trying to go with the notepad affect where once you are finished editing, numbers and such are hyperlinked.
In order to have that notepad effect, where numbers and links are hyperlinked, you would need to replace your UITextView with a special CoreTextView. I'd modify something like this to your needs: https://github.com/jasarien/CoreTextHyperlinkView. So in textViewShouldEndEditing: you would populate your CoreTextHyperlinkView text with textView.text, hide the textView and show your CoreTextView. And vice versa in textViewShouldBeginEditing:.
Also, make sure you reference the <UITextViewDelegate> protocol in your header.
Remember to set your textView's delegate, otherwise those methods won't get called
self.textView.delegate = self; // assuming self is the view controller with your textView
And remember <UITextViewDelegate>

UITextview with zooming?

how can i achieve UITextview with zooming facility?any tutorial please?
You can make simple UIWebView and it will be like UITextView, but with zooming.
This way has a lot of advantages. For example bold text, or italic. You will be able to use different fonts in one text.
Step by step guide:
create UIWebView
generate html string. For example: <html><head></head><body>My text</body></html>
load string in UIWebView
If I'm understanding your question correctly, you want a UITextView, and you want to allow the user to use two fingers to zoom in? If that's the case, when you add the UITextView in IB, at the bottom of the settings for the UITextView there is a check box for "Multiple Touch" just check that box and it will allow the user to zoom in. Hope I understood your question correctly.
If you not need user to edit text and just need to show some text with ability to change font attributes (i.e. size, weight) - use attributed string of text view. Then allow editing attributes and just call methods from UIResponderStandardEditActions protocol, like increaseSize: or decreaseSize: on UITextView. Before it you should set a range of text which must be affected:
self.textView.editable = NO;
self.textView.selectable = NO;
self.textView.allowsEditingTextAttributes = YES;
self.textView.attributedText = [[NSAttributedString alloc] initWithString:#"Lorem ipsum dolor sit er elit lamet..."];
Before calling increase/decrease method select all text:
//First select needed range
[self.textView setSelectedRange:NSMakeRange(0, self.textView.attributedText.length)];
//Then call increazing method
[self.textView increazeSize:nil];
Also you can call other methods of that nice protocol, which UITextView kindly implement:
[self.textView decreaseSize:sender];
[self.textView toggleBoldface:nil];
[self.textView toggleItalics:nil];
[self.textView toggleUnderline:nil];

Resources