How to create Hyperlink on particular textView text in ios - 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;

Related

open a web link from different text in a text view

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;

How to change properties of particular text in a label

I have a label and i set the text of the label programmatically. I want to set one of the word to be bold and the rest normal. However, i am unable to control the properties of the text. For example, I want this "This is an example" but am only able to achieve this "This is an example".
Try this:
NSString *text = #"This is an example";
NSString *textBold = #"example";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString beginEditing];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:20.0f]
range:[text rangeOfString:textBold]];
[attributedString endEditing];
[labelObj setAttributedText:attributedString];
Take a look at the attributedText property of the label. It lets you assign styled text using an NSAttributedString. Explaining how to build an NSAttributedString is beyond the scope of an SO answer, but you should be able to find ample information both in the Xcode help system and online.
Let me show you a demo about the attributedText.
NSDictionary*subStrAttribute1 = #{
NSForegroundColorAttributeName: [UIColor redColor],
NSStrikethroughStyleAttributeName:#2
};
NSDictionary *subStrAttribute2 =#{
NSForegroundColorAttributeName: [UIColor greenColor]
};
NSString *strDisplayText3 =#"Red and Green";
NSMutableAttributedString *attributedText3 = [[NSMutableAttributedString alloc] initWithString:strDisplayText3];
[attributedText3 setAttributes:subStrAttribute1 range:NSMakeRange(0,3)];
[attributedText3 setAttributes:subStrAttribute2 range:NSMakeRange(8,5)];
self.lblInfo3.attributedText= attributedText3;
Since ios6 uilabel supports attributed strings, So you can use it.
For your particular case below code will work-
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"This is an example"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(11, 7)];
label.attributedText = string;

NSMutableAttributedString Crashing App

I'm doing something wrong with the range (I think) in setting this NSMutableAttributedString. Can anyone tell me why this is crashing my program? It looks right to me, but I'm obviously wrong! Thanks.
NSString *placeHolderString = #"USERNAME";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
float spacing = 5.0f;
// crashes on this line
[attributedString addAttribute:NSKernAttributeName
value:#(spacing)
range:NSMakeRange(0, [placeHolderString length])];
self.userNameTextField.attributedPlaceholder = attributedString;
What I think was causing your issue is that you were never really accounting for your placeholderString in the first place. In addition, your value parameter could simply use numberWithFloat as the application would then known what type you are using all the time.
Once you account for the placeHolderString, you are then going to use the length for the attributeString, as it now just contains the contents of your placeholderString. Then we just simply copy that string which contains your attribute using the UITextField property attributedText.
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:placeHolderString];
float spacing = 5.0f;
[attributeString addAttribute:NSKernAttributeName
value:[NSNumber numberWithFloat:spacing]
range:(NSRange){0,[attributeString length]}];
userNameTextField.attributedText = [attributeString copy];
For more context, attributes like NSUnderlineStyleAttributeName exist and you can do some really powerful things with NSMutableAttributedString. Refer to the documentation for options.

How to make bold font NSString value?

In my iOS application, how can I make bold font in NSString value? I used below code for making bold font text in NSString value, but showing error
like this:
Undefined symbols for architecture i386:
"_kCIAttributeName",
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:#"%#%#%#",key,#":",#" "];
NSRange boldedRange = NSMakeRange(22, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCIAttributeName value:boldFontName range:boldedRange];
[attrString endEditing];
NSString *string = [NSString stringWithFormat:#"%#%#",attrString,[dic objectForKey:key]];
How do I make bold font in string?
the NSString is for store a simple texts without any font formatting.
the UILabel has a font property, you can use it to set the visible text's font on the UI, but the UILabel can work with one format only and the whole text gets the same font and style.
if you really want to use a kind of rich text document, you should create a UIWebView class and you can show any document inside with the standard HTML tags.
UPDATE
the UILabel can store attributed string since iOS6, which gives you a chance to show rich-formatted text in one single UILabel instance, even using various fonts or sizes, etc...

Right to left UILabels

I need to display a text using a UILabel ( can't use UIWebView ), and it sometimes contains both Hebrew and English. using UILabel's default settings the sentence gets mixed up and doesn't make sense. I have failed to found a way to make UILabel display text rtl.
Does anybody know anyway to do that, or a code that implements this?
Have a look at this SO, it contains some info on this subject that might help you out.
It seems to work for some by adding the code \u200F to the strings to be displayed.
NSString *RTFstr = "1. בבוקר"; //This could be any right-to-left string
NSString *directionalString = [#"\u200F" stringByAppendingString:[note text]];
[someUITextView setString:directionalString];
This will work
-(void)fnForWritingDirection:(UILabel*)label textFor:(NSString *)stringForText{
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: [stringForText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setBaseWritingDirection:NSWritingDirectionRightToLeft];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
label.attributedText=attrStr;
}

Resources