Insert a hyperlink in UILabel - objective-c

i have a response coming in from the api in form of a text I want to scan that text for the word "Planned Parenthood health center" and insert a hyperlink on that word which redirects to plannedparenthood.org/health-center portal.
my approach:
NSString *text = response.content;
text = [text stringByReplacingOccurrencesOfString:#"Planned Parenthood health center"
withString:#"Planned Parenthood health center"];
though the link on the text is getting replaced. It is getting replaced by
Planned Parenthood health center
What's wrong why am I not getting any links there? am I doing something wrong I have also set the user enabled interaction to YES

iOS does not handle this well to be honest probably because it intends you to use UIButtons which are much more easily tappable for users.
To accomplish what you want though, you cannot use a UILabel - you need to use a UITextView and set its attributedText property:
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:#"Planned Parenthood health center"];
[attributedString addAttribute: NSLinkAttributeName value: #"http://www.plannedparenthood.com" range: NSMakeRange(0, str.length)];
textView.attributedText = attributedString;
Note that the UITextView must be selectable but not editable.
There is a bit of a downside with this method if you have other text in your attributed string that isn't linked. For example, if you have "Please select here in order to go to Planned Parenthood's website" where "here" is the only part of the text that you want linked. In this case, if you tap anywhere else in the text beside the word "here", you'll notice that it will be able to be selected by the user and have the blue highlight.
If using a UITextView is not desired, then you will need to use something custom such as TTTAttributedLabel

Related

Click on UILabel and perform action

I have a simple problem, I have a string like "#my#name#is#umesh#verma" and assign to a UITableview cell label,
cell.detaillabel.text = #"#my#name#is#umesh#verma";
My Problem is how to get each word name when I click on single item.
If I click on #umesh then I get "umesh" word..
More better solution is add custom label which supports touches. For example TTTAttributedLabel supports touches on links.
Main task is get notification when user touch a word and to identify the word.
You can add URLs (with special format) to any word and subscribe to a notification when user click it (as delegate to the label). For example you can create this URL for "word":
touchscheme://word
I haven't checked about how to perform an action when clicking on a UILabel. However, I experienced that with UITextView. You can use "NSLinkAttributeName" to do that.
Basically, from your original string, try to find the range of string that you need to trigger actions. Then add a link value.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:yourString];
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
[textView setAttributedText:attributedString];
Set delegate to your textView and handle the following method
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
// You can retrive your string here or perform an action
return YES;
}
Hope this would be helpful for you.

iOS - Only first line of text is viewable

In two places in our app the text that the user types in only shows first line of text. Both occurances are in external frameworks, first in UIActivityView, the other in Freshdesk MobiHelp.
First, with UIActivityView, when using Twitter:
The problem is that if the text goes beyond one row in the modal, the text goes transparent:
NSString *textToShare = [NSString stringWithFormat:NSLocalizedString(#"CHALLENGE-TWITTER- DEFAULT-TEXT", nil), [UserManager currentUser].displayName];
NSString *urlToShare = [NSURL URLWithString:#"http://example.com"];
NSArray *activityItems = #[textToShare, urlToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
Second, in Freshdesk submit a ticket:
I should also add that the Facebook modal from UIActivityView works just fine:
Would really appreciate any tips here, as I'm lost.
I think in the second sreenshot that you have attached here, there is a white screen over a red area which probably might be your label or the text area and I guess its blocking your text area. If the view was added by you, you can get the particular text area to front so that its not blocked and you can see your text. Hope this helps
Can you see a white box inside the one I highlighted? There is some view over your textarea. How are you adding your textarea? As a subview?? Can you just get log the main views subviews?? Like
NSLog (#"%#",[self.view subviews]);
And now check the view hierarchy.

NSTextFinder set search string and clear visual feedback programmatically

I have an NSTextView that uses the find bar ([textView setUsesFindBar:YES];).
I have 2 questions.
How do I clear the visual feedback from a find operation?
My problem happens when I programmatically change the content of the textView. The visual feedback for a search operation on the previous content remains after the content change. Obviously these yellow boxes do not apply to the new content so I need a way to clear them when changing the textView content.
Note: I did not implement the NSTextFinderClient protocol because I have a simple textView and the find bar just works without any other effort.
How can I send a search string to the find bar?
I found my answers, so for others here's how to do it.
First you need an instance of NSTextFinder so you can control it. We set that up in code.
textFinder = [[NSTextFinder alloc] init];
[textFinder setClient:textView];
[textFinder setFindBarContainer:[textView enclosingScrollView]];
[textView setUsesFindBar:YES];
[textView setIncrementalSearchingEnabled:YES];
First answer: To clear visual feedback I can do either of 2 things. I can just cancel the visual feedback...
[textFinder cancelFindIndicator];
Or I can alert NSTextFinder that I'm about to change my textView content...
[textFinder noteClientStringWillChange];
Second answer: There's a global NSFindPboard. You can use that to set a search.
// change the NSFindPboard NSPasteboardTypeString
NSPasteboard* pBoard = [NSPasteboard pasteboardWithName:NSFindPboard];
[pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, NSPasteboardTypeTextFinderOptions, nil] owner:nil];
[pBoard setString:#"new search" forType:NSStringPboardType];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSTextFinderCaseInsensitiveKey, [NSNumber numberWithInteger:NSTextFinderMatchingTypeContains], NSTextFinderMatchingTypeKey, nil];
[pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions];
// put the new search string in the find bar
[textFinder cancelFindIndicator];
[textFinder performAction:NSTextFinderActionSetSearchString];
[textFinder performAction:NSTextFinderActionShowFindInterface]; // make sure the find bar is showing
There's a problem though. The actual text field in the find bar does not get updated after that code. I found that if I toggle the first responder then I can get it to update...
[myWindow makeFirstResponder:outlineView];
[myWindow makeFirstResponder:textView];

UITextView character wrap but not word wrap

I have a UITextView that I would like to disable word wrap on but keep character wrapping enabled.
Basically I would like to have a long string 200+ characters that still wraps in the textview but prevents word recognition and/or word wrap. Is this possible and if so can someone point me in the right direction?
Thanks!
This is not possible with a stock UITextView, because there is no lineBreakMode property in its public API. If you don't need the text to be editable, you could look into other solutions such as a UILabel or Core Text. Getting this behavior in an editable control is possible, but will probably be a pretty hard slog.
you can try
NSMutableAttributedString *att=[[NSMutableAttributedString alloc]initWithString:str];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[att addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];

NSTextField - make selected text bold, italic or underline?

I have an NSTextField where the user can write text. I would like to be able to make 3 buttons: bold, italic and underline; these buttons should change the user selection in the textfield to either bold, italic or underline.
Can anyone give me a pointer on how to do this?
The first thing is to enable rich text support, and you can do it either in Interface Builder by checking the "Rich Text" option in the inspector or by code using setAllowsEditingTextAttributes:.
Then it's all about NSAttributedStrings.
The big problem though is that looks like you need to apply changes to the selected text. This is not possible with NSTextFields. Only with NSTextViews.
If you can change it, go ahead and it will make things easier. However, if you do need to stick with NSTextField you may want to access the field editor. Each window has one associated, and it's what process the text behind the scenes.
NSTextView *editor = (NSTextView *)[window fieldEditor:YES forObject:myTextField]
Then you can call NSTextView's method setSelectedTextAttributes: happily.
Read more about the field editor here at Apple and in CocoaDev
Assuming your NSTextfield * is textField, the code below underlines the selection:
NSMutableAttributedString * as = [[[textField attributedStringValue] mutableCopy] autorelease];
[as beginEditing];
[as addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:[[[textField window] fieldEditor:YES forObject:textField] selectedRange]];
[as endEditing];
[textField setAttributedStringValue:as];