Two hyperlinks in UITextView with different colors? - ios

I am trying to make a UITextView to have two links in it as text, but with different colors. I am not sure if it's possible at all.
I have set the UITextView to detect links, not to be editable, but only selectable.
What I did so far is the following:
NSMutableAttributedString *termsAndConditionsTitle = [[NSMutableAttributedString alloc] initWithString:#"I have read the " attributes:#{NSLinkAttributeName: #"https://apple.com", NSForegroundColorAttributeName: [UIColor greenColor]}];
NSMutableAttributedString *someString2 = [[NSMutableAttributedString alloc] initWithString:#"terms and conditions" attributes:#{NSLinkAttributeName: #"https://microsoft.com", NSForegroundColorAttributeName: [UIColor redColor]}];
[termsAndConditionsTitle appendAttributedString:someString2];
[self.termsAndConditionsView setAttributedText:termsAndConditionsTitle];
But in the end the links just have the tint color of the UITextView. Is it possible to make the two link have different colors? Thanks in advance!
P.S. I don't want to use libraries/frameworks if possible.

Yes it is possible, the problem is that the TextView has some predefined attributes for the links.
To fix it you just have to remove them in this way:
yourTextView.linkTextAttributes = [:]
With this line of code if the textview detects a link it won't apply any special attribute to it. So it will work as usual but without changing the color of the links. If you want to change the colors, you'll have to do it manually when you are adding the attributes.

You'll need to assign your text view's linkTextAttributes - basically you just tell your text view to identify links and stylize them however you specify:
Objective-C
textView.linkTextAttributes = #{
NSForegroundColorAttributeName:[UIColor someColor],
NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle)
};
You should also be able to assign the tintColor (since UITextView's inherit the same behavior as UIView's and will use this color for links). But if that isn't working for whatever reason in your case, assigning the link attributes will be the solution.
If your intention was to have two different colors for each link, then you will have to manually assign the attributes of each link based on the range at which each link occurs in your string.

Related

How to show two separate links on different Strings in UILabel?

Please look at the below image, Red Color Texts are two different strings and Ash color String are another string. I concatenate those strings into one String and i assigned that string into UILabel.
i used NSMutableAttributedString for applying colors and Font styles to NSString
Now i need to show two separate Links on the Red Color Strings " SIVASAGAR" and "THE ORDER:1886", when user clicks on that links, it redirects to different views.
Is it possible to achieve this type of scenario using UIButton or STTweetlabel or something else?
NOTE: All the names ,i mean Red Color Strings Positions(X and Y values) will change Dynamically according to requirement.
I think you should use UITextView and add:
[attributedText addAttribute:NSLinkAttributeName value:#"yourCustomSchemeUrl://" range:linkRange];
and implement Custom URL Scheme

Make part of an alertview bold

Lets say I want to make only the text "Software" bold. Is there a way to achieve this without a custom alertview?
self.subHeadingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
Check out dynamic text in ios7 . it has six different font styles. There is a good article here

iOS: URL link in UITextField with different text

I want to insert a URL hyperlink into a UITextField that has different display text.
This is super easy in html:
Go To Google
How can I do this in iOS?
You can change the text style inside your UITextField via an attributed string sent to the text field's "attributedText" property.
However, what I suspect you really want to do is have a link that's clickable. I would recommend using a UIButton with a custom type (i.e. no border) and you can set the color and the underline style.
But like Evan said, if you have multiple lines of text, it may be smarter to use a UITextField where you set "editable" to NO and turn on the LINK traits (both of these you can do from the object inspector in Xcode).
Alright, so here's what I did to get what I wanted. I used UIWebView, and simply linked it to an html page in the project that has the text, and hyperlink at the bottom with different text displayed.
Answer is here :
If you want it as clickable Hyperlink,
NSString *string = #"Go To Google";
You need to add "BACKWARD SLASH" before ". That's it.

Coloring links with multiple various colors in the same NSAttributedString

I'm using TTTAttributedLabel to detect clicks on links in a styled UILabel (using NSAttributedString), in an iOS 6 project. I'd like to be able to have alternating colors for links in my label; I'm fine with manually setting the different colors for different link ranges, as long as the library handles link-detection with user touches for me. It seems that the TTTAttributedLabel class applies link styling last, so that text styling for specific ranges is overwritten by the single link style set for the class instance.
Being about to dive in and try to modify the TTTAttributedLabel code (to either not apply link styling, or to apply my own style ranges afterwards), I figured I'd ask here whether anyone has better ideas to consider for achieving this. Might a different library support variously-colored link ranges in the same label, out of the box?
This is already supported, simply use:
- (void)addLinkWithTextCheckingResult:(NSTextCheckingResult *)result
attributes:(NSDictionary *)attributes;
This lets you specify your own attributes on a per-link basis. E.g., with a linkAttributes dictionary for one-off coloring of a link:
if (linkAttributes) {
[self addLinkWithTextCheckingResult:[NSTextCheckingResult linkCheckingResultWithRange:linkRange URL:[NSURL URLWithString:linkText]] attributes:linkAttributes];
}
else {
[self addLinkToURL:[NSURL URLWithString:linkText] withRange:linkRange];
}
The link attributes dictionary uses keys defined in NSAttributedString.h. For example:
linkAttributes = #{
NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineStyleAttributeName: #(NSUnderlineStyleNone)
};

iOS: custom font for specific label

I looked at a few examples here on OS and also followed this one but what I wanted to know is how I can set a custom font for only one specific label.
This line is supposed to change the font:
[UIFont fontWithName:#"Cloister Black" size:64.0]
But I don't want everything to be affected. Any ideas how I do this?
Setting the font on a label is as easy as modifying that label's "font" property (and I've linked Apple's documentation for you).
E.G.:
labelIWantToModify.font = [UIFont fontWithName:#"Cloister Black" size:64.0];
And I'm hoping you're referring to that one specific label, and not a piece or part of the string that appears within the label, which is a separate (attributed string) thing.

Resources