open a web link from different text in a text view - ios

I have an iOS app in which the text from a text view is too long to fit. So I am trying to link the web address to the text in the textView.
Here is what I have.
NSURL *URL = [NSURL URLWithString:#"azgovernor.gov/engage/form/contact-governor-ducey"];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Email Governor"];
[str addAttribute: NSLinkAttributeName value:URL range: NSMakeRange(0, str.length)];
govemail.attributedText = str;
The Email Governor shows up in the TextView as hyperlink(blue link) and when I touch it, it reacts.
However, the link doesn't respond and I get this message:
Could not find any actions for URL azgovernor.gov/engage/form/contact-governor-ducey without any result.
If I put this address into Safari it opens the page fine. What do I need to do to make this work?

try this:
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"http://azgovernor.gov/engage/form/contact-governor-ducey"];
govemail.dataDetectorTypes = UIDataDetectorTypeLink;
govemail.editable = NO;
govemail.attributedText = str;

Related

How to create Hyperlink on particular textView text in ios

NSString *str = #"We’ll notify you before we make changes to these
terms and give you the opportunity to review and comment on the revised
terms before continuing to use. If you have any question just email
me.";
NSMutableAttributedString *aStr = [[NSMutableAttributedString
alloc]initWithString:str attributes:nil];
[str rangeOfString:#"email"]];
[self.textView setAttributedText:aStr];
this is my text i want create link on email.
you can do something like,
NSString *str = #"We’ll notify you before we make changes to these terms and give you the opportunity to review and comment on the revised terms before continuing to use. If you have any question just email me.";
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:#"Your Link here" range:[str rangeOfString:#"email"]];
[self.textView setAttributedText:aStr];
Update
You can add font attribute to make it bold. For example if you want to make We’ll notify bold then
[aStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14.0] range:[str rangeOfString:#"We’ll notify"]];
add this line before you set attributed text to your textView !
Add this property to your textview
textview.dataDetectorTypes = UIDataDetectorTypeLink;

Out of bound error adding link to nsattributedstrings in iOS

I have the following code setup in iOS using objective C, however the part addint the link only works for ranges starting at 30 or less. The length of short_content should always be at least 100 words.
NSString *short_content = [self getFirstNWords:100 :content];
NSString *link = #" \nClick here to read more ";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[short_content stringByAppendingString:link]];
[str addAttribute: NSLinkAttributeName value:#"http://mytest.com/" range:NSMakeRange(str.length - 100, short_content.length)];
If you want to set the range of the "link" part of the string, then you want:
[str addAttribute: NSLinkAttributeName value:#"http://mytest.com/" range:NSMakeRange(str.length - link.length, link.length)];

Display Emoji from unicode in UILabel in iOS

I am working on emoji in chat app.
When someone send me emoji in message it look like this type :- Hello...(worried) how are you(happy)?. Here (worried)and (happy) are assigned keys for emojis.
This is list for emojis and key and value.
dictemoji = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"worried-48.gif",#"(worried)",
#"sad-48.gif",#"(sad)",
#"bandit48.gif",#"(bandit)",
#"wink48.gif",#"(wink)",
#"surprised48.gif",#"(surprised)",
#"smirking48.gif",#"(smirking)",
#"laugh48.gif",#"(laugh)",
#"cool48.gif",#"(cool)",
#"stoned-48.gif",#"(stoned)",
#"smile-48.gif",#"(smile)",
#"nerd-48.gif",#"(nerd)",
#"happy-48.gif",#"(happy)",
#"evil-grin-48.gif",#"(evil-grin)",
#"tongue48.gif",#"(tongue)",
#"lips-sealed-48.gif",#"(lips-sealed)",
#"GIF48.gif",#"(GIF)",
#"dull48.gif",#"(dull)",
nil];
When I received message Hello...(worried) how are you(happy)? I want to saw my emoji instead of (worried)and(happy) in label.
So how can I take emoji instead of those words?
EDIT:-
When someone send me emoji with text message, it will replace with dictionary value :
for (NSString *emojiKey in dictemoji.allKeys)
{
if ([message containsString:emojiKey])
{
message = [message stringByReplacingOccurrencesOfString:emojiKey withString:[dictemoji valueForKey:emojiKey]];
}
}
// helllo(sad)...how are you(smile)...? ----->it will look like helllo(sad-48.gif)...how are you(smile-48.gif)...?
NSLog(#"message updated:%#",message);
cell.textLabel.text=message;
So, I want to display emoji where (sad-48.gif) and (smile-48.gif) printed in label.
Try this. It may help you.
Add one label & write the following code in viewDidLoad :
NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];
_lbl.text = valueEmoj;
You have to use like this ;
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:[dictemoji valueForKey:emojiKey]];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"];
[myString appendAttributedString:attachmentString];
cell.attributedText = myString;
hope this will help.

Make the text hyperlink or clickable in UITextView

I am having the UITextView and displaying text in it from three tags(message, titleUrl and url). What i need is that i want to make the text of "titleUrl" clickable to open the value of "url" in web view. I managed to open the link directly from url, but i need to open the link by clicking "titleUrl". I have tried to achieve the following from this code.
[self buildAgreeTextViewFromString:NSLocalizedString(#"I agree to the #<ts>terms of service# and #<pp>privacy policy#",
#"PLEASE NOTE: please translate \"terms of service\" and \"privacy policy\" as well, and leave the #<ts># and #<pp># around your translations just as in the English version of this message.")];
But i am not getting in this how to modify this to achieve the functionality. I want to enter the value have in string, don't have the static text to enter. Can anyone guide me to handle this?
Update:
NSString *message = [NSString stringWithFormat:#"%#\n ", tempStr1];
NSString *message1 = [NSString stringWithFormat:#"\n#<pp>%##", titlStr1];
NSString *localizedString = NSLocalizedString(message1, nil);
NSRange ppRange = [localizedString rangeOfString:NSLocalizedString(message1, nil) options:NSCaseInsensitiveSearch];
NSURL *ppURL = [NSURL URLWithString:strUrl];
NSDictionary *attribute1 = #{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:15.0],
};
NSMutableAttributedString *newAttString = [[NSMutableAttributedString alloc] initWithString:message attributes:attribute1];
//
NSMutableAttributedString *finalMessage = [[NSMutableAttributedString alloc] initWithString:localizedString];
[finalMessage beginEditing];
[finalMessage addAttributes:attribute1 range:ppRange];
[finalMessage addAttribute:NSLinkAttributeName value:ppURL range:ppRange];
[finalMessage endEditing];
[newAttString appendAttributedString:finalMessage];
self.txtView.attributedText = newAttString;
This is as simple as using an NSMutableAttributedString. Note: This is not the only way, this can be done with searching for ranges etc, this is just a simple implementation to get you in the right direction since you do have the static message, because your localizing all of them, which means you have the static english form of it.
NSString *tosString = #"Terms of Service";
NSString *ppString = #"Privacy Policy";
NSString *message = [NSString stringWithFormat:#"I agree to the #<ts>%## and #<pp>%##", tosString, ppString];
NSString *localizedString = NSLocalizedString(message, nil);
NSRange tosRange = [localizedString rangeOfString:NSLocalizedString(tosString, nil) options:NSCaseInsensitiveSearch];
NSRange ppRange = [localizedString rangeOfString:NSLocalizedString(ppString, nil) options:NSCaseInsensitiveSearch];
NSURL *tosURL = [NSURL URLWithString:#"http://toslink.com"];
NSURL *ppURL = [NSURL URLWithString:#"http://pplink.com"];
NSMutableAttributedString *finalMessage = [[NSMutableAttributedString alloc] initWithString:localizedString];
[finalMessage beginEditing];
[finalMessage addAttribute:NSLinkAttributeName value:tosURL range:tosRange];
[finalMessage addAttribute:NSLinkAttributeName value:ppURL range:ppRange];
[finalMessage endEditing];
self.yourTextView.attributedText = finalMessage;
Swift 3.0
in your view did load...
let tosString = "Terms of Service"
let ppString = "Privacy Policy"
let message = "By logging in, you agree to our (tosString) and that you have read our (ppString)"
let localizedString = NSMutableAttributedString(string: message)
let tosRange = localizedString.mutableString.range(of: tosString)
let ppRange = localizedString.mutableString.range(of: ppString)
let tosURL = URL(string: "http://toslink.com")!
let ppURL = URL(string: "http://pplink.com")!
localizedString.addAttribute(NSLinkAttributeName, value: tosURL, range: tosRange)
localizedString.addAttribute(NSLinkAttributeName, value: ppURL, range: ppRange)
demoTextView.delegate = self
demoTextView.isSelectable = true
demoTextView.isUserInteractionEnabled = true
localizedString.endEditing()
self.demoTextView.attributedText = localizedString
and using textview delegate method
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
// Handle your Control
return true
}
Look into using an attributed string with NSLinkAttributeName.

UITextView Not Opening URL When Clicked

I have a UITextView that has an attributed string inside, which contains a URL. The attributed string is being converted to a link, but when you press the link you get an error:
Unknown DDResult category 1 for result ; could not find any actions for URL www.myurl.com
Here is my code:
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(#"My Text", nil)];
NSRange range = [str.mutableString rangeOfString:#"www.myurl.com" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"CourierPrime-Bold" size:15] range:NSMakeRange(0, range.location)];
[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:#"www.myurl.com"] range:range];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range];
}
self.welcomeText.editable = NO;
self.welcomeText.attributedText = str;
//As a note, I have tried this with the value 0 and UIDataDetectorTypeLink
//and neither option worked.
self.welcomeText.dataDetectorTypes = UIDataDetectorTypeAll;
I am using an XIB and in the behavior options I have selectable enabled, and in the detection options I have links selected.
I would like for the link to be opened in the phones mobile browser if possible, rather than creating a webview.
The URL needs a scheme.
This:
[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:#"www.myurl.com"] range:range];
needs to be:
[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:#"http://www.myurl.com"] range:range];
In Swift :
The URL needs to be Secure (https)
IOS doesn't allow you to redirect the user to non secure links , so provide links with https
and to make a specific string clickable in UITextView using Swift
you can do as follow :
let link = "https://www.google.com"
let range = (text as NSString).range(of: link)
attribute.addAttributes([NSAttributedStringKey.link: link], range: range)

Resources