UITextView crash when clicking on a URL - ios

I have the following lines in my code to add a clickable url to a UITextView. merchantwebsite is my UITextView.
self.merchantwebsite.attributedText = [[NSAttributedString alloc] initWithString:#"http://www.crossfit.com" attributes:#{NSLinkAttributeName: #"http://www.crossfit.com"}];
self.merchantwebsite.userInteractionEnabled = YES;
When I click on the UITextView, the app crashes with the log
2014-03-19 16:13:43.051 BTLE[27103:60b] -[__NSCFConstantString scheme]: unrecognized selector sent to instance 0x1a4404
Can someone please tell me what i'm doing wrong.
Thanks!

You evidently have code that expects the URL to be an NSURL. But, quite simply, an NSString is not an NSURL. Try it like this:
self.tv.attributedText =
[[NSAttributedString alloc]
initWithString:#"http://www.crossfit.com"
attributes:
#{NSLinkAttributeName: [NSURL URLWithString:#"http://www.crossfit.com"]}];

Just type the link in when you are telling the UITextView what to display and then turn on link recognition in the settings on the right when your UITextView is selected from the .storyboard page.
You don't really need any of the code that you have all you need is:
merchantwebsite.text = #"http://www.crossfit.com";

Change your code as follows:
NSAttributedString *string = [[NSAttributedString alloc] initWithString:#"http://www.crossfit.com" attributes:#{NSLinkAttributeName: #"http://www.crossfit.com"}];
[self.merchantwebsite setAttributedText:string];

Related

Why is NSTextAttachment image not displayed?

I'm trying to add an image to an attributed string:
NSTextAttachment *locationIcon = [[NSTextAttachment alloc] init];
locationIcon.image = [UIImage imageNamed:#"location-pin-icon"];
NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:locationIcon];
[string appendAttributedString:iconString];
NSAttributedString *locationNameString = [[NSAttributedString alloc] initWithString:#"some text" attributes:linkAttributes];
[string appendAttributedString:locationNameString];
Then I simply set myLabel.text = string; (where myLabel is a TTTAttributedLabel)
location-pin-icon is a valid image (I've checked it in debugging too). However, the location pin icon is not being displayed in the label (the following "some text" is displayed perfectly though, and linkAttributes is just a collection of system font with a custom blue color). I've also tried manually setting bounds to the text attachment, or leaving a space before the text, but nothing seems to work.
What am I doing wrong?
It was apparently due to implementation of TTTAttributedLabel. The library seems broken. When I've switched from it, the attachment started displaying correctly.

How could I change the color of a fixed character of placeholder text, as I am bit confused to perform this

I tried all the ways but its not as per my aspectations please help.
I have to change the color of all * to red.
I know I have to work in didload event.
I tried to get the last character of all the strings and do one by one but it makes the code quite lengthy hope there would be any should code/ approach.
Thanks
Image Here Please have a look
Try this one
NSMutableAttributedString *placeHoldertString = [[NSMutableAttributedString alloc] init];
NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:#"First Name" attributes:#{ NSForegroundColorAttributeName : [UIColor lightGrayColor] }];
NSAttributedString *str2 = [[NSAttributedString alloc] initWithString:#"*" attributes:#{ NSForegroundColorAttributeName : [UIColor redColor] }];
[placeHoldertString appendAttributedString:str1];
[placeHoldertString appendAttributedString:str2];
yourTextFeild.attributedPlaceholder = placeHoldertString;
your question is still not clear you could make the textfield red on two events 1). when the user is entering the characters and jumps to the next textField you can make it red if the input is blank or incorrect
2). You can make it red on button click of submit or anything you want
and instead of writing it in View did load write it in UitextField Delegate method i.e Should change character in range method this will be called for each text field and you can use nested if else for your text field inside that method
e.g
if textField == (your textfiled name here)
{
//do your logic here for making the field red
}
remember you have to use this nested if else in should change character in length method UitextField Delegate
Try this
textField.attributedPlaceholder =
[[NSAttributedString alloc] initWithString:#"yourPlaceHolderName" attributes:#{NSForegroundColorAttributeName: yourColor, NSFontAttributeName : yourFont}];
}

When tapping a custom URL link in a UITextView the delegate receives nil URL

I have a tableView with tableViewCells cell that show a textView.
textView uses an attributedString with custom URL link information, set up in tableView:cellForRowAtIndexPath: as shown in this tutorial:
NSMutableAttributedString *attributedDisplayString = [[NSMutableAttributedString alloc] initWithString:displayString];
[attributedDisplayString addAttribute:NSLinkAttributeName
value:[NSString stringWithFormat:#"username://%#", userName]
range:NSMakeRange(0, userName.length)];
cell.textView.attributedText = attributedDisplayString;
When I tap the link, textView:shouldInteractWithURL:inRange: is called in the delegate, thus the custom URL link has been detected and responds.
However, the supplied URL is nil.
What am I missing?
Sorry for asking too fast. I found the problem, but maybe this helps others:
My variable userName simply contained a space, and could thus not be converted to a URL.
After removing the space, it works.
To make a string that can be used for a URL, one can use in iOS8 and earlier
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
and in iOS9
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]

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