Create dynamic cell with link. - ios

I'm developing application similar to Vine Application, in that comment cell is giving navigation (#User is there or #tag is there) that is exactly working if user or tag is there otherwise not.
If anyone know, how to do that please give me reference or suggestion (Please don't give any suggestion like if word found with # or # then give string attribute that I already done that but I want exactly vice comment cell )
You can give some idea or suggestion also.
Thanks is advance.

You can use TTTAttributedLabel. It will help you to provide link action.
Implement it as follows
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:frame];
[label setAttributedText:#"yourText"];
label.delegate = self;
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
[label addLinkToURL:url withRange:range];
[yourCell.contentView addSubview:label];
and you can capture the link action in:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
which is the TTTAttributedLabelDelegate method. Don't forget to add TTTAttributedLabelDelegate in the interface file.

Related

Swift : How to open a link when tap on text in label?

I want to add a link to some text. At the moment the text is in a UILabel. I want the link to be on the text so the user would not visually see the link. How can this be done?
In web development it looks like this.
Example 1
UILabel is not made for that, but you can link the text with an action (like tapping) in order to do whatever you like. Don't know about swift but in Objective-C is something like:
// If you have UILabel* myLabel
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myAction:)];
[myLabel addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
gr.cancelsTouchesInView = NO;
And then you can add the action:
- (void) myAction: (UITapGestureRecognizer *) gr {
// Code here
}
Maybe you can figure out how to do this in swift
You can also make a button look invisible. This could create the effect you are looking for.

iOS Programming:Correcting Text

So I've been developing an iOS application, and one part of it involves the user entering a paragraph of text, and I need my app to filter the text, and use Apple's autocorrect function, to rectify mistakes in the text. For example, if the text is-
The quick brown fox jumpet over the lazy dog
Then it should be able to take the word 'jumpet' and change it to jumped. Does anyone know how this can be done? And I don't have to prompt the user, I'm planning to run this code in a background thread, while an activity indicator spins.
Thanks A Lot!
Raghav
P.S. - The text is in an NSString...
You can manually check spelling in a string using UITextChecker.
I haven't used it myself but it looks pretty straightforward.
Seems Apple doesn't give a public API for autocorrections,
So you should do this with some tricks and hacks.
This article should be useful.
http://blog.persistent.info/2013/10/programmatically-accepting-keyboard.html
Copied From here
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// Turn spell check on
textView.autocorrectionType = UITextAutocorrectionTypeYes;
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
// Turn spell check off and clean up red squiggles.
textView.autocorrectionType = UITextAutocorrectionTypeNo;
NSString *currentText = textView.text;
textView.text = #"";
textView.text = currentText;
return YES;
}

Font Size Resizes by Stepper

I want my app to be when you click the stepper it will resize the font in a UITextView. The problem is I'm getting errors.
- (IBAction)myStepper:(id)sender {
[myStepper setMinimumValue:14.0]
self.myStepper.maximumValue =20.0;
UIFont newSize = [myTextView fontWithSize:self.stepper.value];
self.myTextView.font = newSize;
}
This all of my code, am I missing something?
You are missing a self and a semicolon in the first line:
[self.myStepper setMinimumValue:14.0];
And an asterisk and self in this line, and stepper should be myStepper:
UIFont *newSize = [self.myTextView fontWithSize:self.myStepper.value];
I would clean your method a bit like this:
- (IBAction)myStepperValueChanged:(UIStepper *)sender {
[sender setMinimumValue:14.0];
[sender setMaximumValue:20.0];
UIFont * newSize = [UIFont fontWithName:myTextView.font.fontName size:sender.value];
[self.myTextView setFont:newSize];
}
but I'd put the first two lines into the -viewDidLoad method as part of the standard init procedure, if they are static values in runtime using the outlet's name (I assume it is myStepper in your class):
[myStepper setMinimumValue:14.0];
[myStepper setMaximumValue:20.0];
or I'd set these values in the Interface Builder, and you'd not need to deal with any outlet for your stepper at all.
NOTE: it is hard to tell which solution would fit better for you, you have not shared too much information which could help me to recommend a specific solution.
For easier code-reading, you shouldn't give your action method the same name of your UISteppert property. Change it to -(IBAction)stepperValueChanged:(id)sender for example.
You don't need to set the min and max value in this method, but in your initialization methods
- (void) viewDidLoad
{
[super viewDidLoad];
// Various implementation
self.myStepper.minimumValue = 14;
self.myStepper.maximumValue = 20.0;
}
- (IBAction) stepperValueChanged:(id)sender
{
self.myTextView.font = [UIFont fontWithSize:self.myStepper.value];
}
It looks like you are some issues understanting the differences between method and properties, you should read some documentation and tutorials if you're beginning development (and welcome to the development world ;) ).
EDIT :
The property myStepper doesn't exists in your class. If you use interface builder, you have to link your stepper to an UIStepper property : this apple tutorial will help you : https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-connections_bindings/CreatingOutlet.html .
Otherwise, if you don't use Interface Builder, you must add an UIStepper property to your class and allocate it by yourself.

How to clear a UITextView with a custom NSTextStorage? [iOS7 only]

I'm currently trying to create an SMS-like screen, where the user can write some text and send it to other users. Everything goes as expected until I try to clear my text view and encounters a crash.
I've been trying to find a way around this issue, but I just cannot find enough documentation online. So here it is, and hopefully one of you will know a fix for this.
The implementation
My UITextView is a subclass of Peter Steinberger's implementation for iOS7, and I use it with a custom NSTextStorage subclassed as showed in objc.io's 'Getting to Know TextKit' and especially that source code in order to highlight usernames in the message.
In my ViewController, I configure my text storage like this:
self.textStorage = [[[MyCustomTextStorage alloc] init] autorelease];
[self.textStorage addLayoutManager:self.textView.layoutManager];
And then in my TextView's delegate method:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
I store the input in my custom TextStorage:
[self.textStorage.string stringByReplacingCharactersInRange:range withString:text];
The crash
I can retrieve my text via self.textStorage.string, and then I clear my text view by replacing characters in the range of the input string. This works quite well, but when I try to set my TextView as the first responder again, the application crashes.
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {5, 0} out of bounds; string length 0'
The range mentioned is the range of my previously cleared string so it looks like I can clear the display, but my TextStorage / TextView is keeping a reference to the first edited range / string.
Any idea on what could be causing this crash and how to resolve it?
Thanks for your attention; at this point any help is appreciated, so please feel free to post if you have some piece of advice. :)
The reason it's crashing is because you are using a range that it outside the bounds of the string in the textStorage. If you want to clear the textView why not just use self.textStorage.string=#""
Same situation with you.
It costed me half a day to solve it.
The example you provide is right.
Your app will crash not only when you are clearing it, but also when using -setText: every time.
They don't crash beacause they would never call -setText:!!!
Solution:
add all TextKit staffs you need to your code,
like this:
_textStorage = [[NSTextStorage alloc] init];
_layoutManager = [[NSLayoutManager alloc] init];
[_textStorage addLayoutManager:_layoutManager];
_textContainer = [[NSTextContainer alloc] init];
[_layoutManager addTextContainer:_textContainer];
_myTextView = [[UITextView alloc] initWithFrame:frame textContainer:_textContainer];
Actually, you missed the NSTextContainer, so there's no right container for string to put in, then the app crashed.
Maybe you can send pull requests to these two demo project, for not miss leading other people.
After replacing text, try:
[self setSelectedRange:NSMakeRange(0, 0)];

Add url to OHAttributedLabel but shouldFollowLink delegate is not called

I add URL to attributed string in OHAttributedLabel ("forgot password" link to www.xxx.com ") . Everything looks perfect except that when I clicked on that string , nothing happen.
It should open that URL in safari. And then when I tried to add delegate method.
-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo
This delegate method is not called at all. The delegate is set properly because I tried another delegate method and it is working
-(UIColor*)attributedLabel:(OHAttributedLabel*)attributedLabel colorForLink:(NSTextCheckingResult*)linkInfo underlineStyle:(int32_t*)underlineStyle;
I setup UILabel via Interface builder and change its class to OHAtrributedLabel. And here is my code
//forgotLabel.text is "Forgot Password"
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:forgotLabel.text];
[attr setLink:[NSURL URLWithString:#"http://www.yahoo.com"] range:NSMakeRange(0, attr.length)];
//forgotLabel is OHAttributedLabel
forgotLabel.attributedText = attr
forgotLabel.delegate = self;
Can somebody help ? I don't want to use TTTAttributedLabel because in my project I used OHAttributedLabel in tablecell and it work perfectly. So I don't want to add another 3rd part compnonent that does almost exactly the same thing .
Thanks in advance.
I solved this myself by adding
forgotLabel.catchTouchesOnLinksOnTouchBegan = YES;

Resources