Different Text Attributes per Link in UITextView/UILabel Swift - ios

I would like to style each link differently within either a UITextView or UILabel. I am currently using UITextViews with links but I want URLs to appear purple and usernames to appear black. I've also looked into TTTAttributedLabel but cannot seem to find out how to accomplish this.
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
attributedString.setAttributes([NSLinkAttributeName: url, range: range)
attributedString.addAttributes([NSForegroundColorAttributeName: UIColor.purple], range: range)
In the example above, which is what I have tried, the link will be orange, not purple. If I do not set linkTextAttributes, links will be blue. How do I override linkTextAttributes with different attributes per link?

linkTextAttributes can't designate the text behavior for each word.
Have you try this?
https://github.com/rinov/RegeributedTextView
and
textView.addAttribute("text1", attribute: .linkColor(.orange))
textView.addAttribute("text2", attribute: .linkColor(.purple))
NSRange is automatically calculated.

Related

Two hyperlinks in UITextView with different colors?

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.

get the attributed string from uitextview

I need to get the attributed string from UITextView. For example if user has formatted the text to bold or italic i need to save in variable or display in label as it is. Please guide me how to do this.
Thanks in advance!
I'm not sure what you're asking here, but UITextView has an attributedText property, so you can just get this by doing:
yourTextView.attributedText
Remember that an attributed string doesn't have only one attribute for the whole string (like an NSString), but depending on the index, it can have different attributes (so the first word can be in bold, and the second one can be italic).
To retrieve an attribute on a given index you can use the following property:
attributedString.attribute("your attribute", atIndex: yourIndex, effectiveRange: yourRange)
You can see more ways of accessing the attributes here.
You could do so:
UITextView *txtView;
UILabel *label;
self.label.attributedText = self.txtView.attributedText;

How to fix UILabel padding issue?

I have a UILabel and whenever I set its outlet the padding I created in Storyboards for it under attributed text disappears like it was never there. The text the stays stuck on the left side. How can I fix this issue?
#IBOutlet weak var mycoollabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mycoollabel.text = "Wow"
}
Note: OP clarified in a comment that they are setting their padding using attributed text in Interface Builder.
The problem here is a confusion between text and attributed text, or, because these are code, it's text and attributedText.
On iOS, lots of things have a text property, and it's just a simple string of text that gets displayed somewhere without any sort of formatting. Usually the formatting is attached to the object that is showing the text, e.g. a label has a text colour and alignment properties that make its text look however you want.
On iOS, most (all?) of those things also have an attributedText property, which is a very different beast. Attributed text contains a string but also specific formatting instructions required to display that string – text attributes, hence the name.
So, OP is creating an attributed text configuration in their storyboard, then, in code, modifying the text property. This will overwrite their formatted string with a plain string, losing its layout configuration.
If you're looking to create an attributed string with a first line indent, you should try this:
let ps = NSMutableParagraphStyle();
ps.firstLineHeadIndent = 50
let attrs = [NSParagraphStyleAttributeName: ps]
let attributedString = NSAttributedString(string: "Hello, world!", attributes: attrs)
label.attributedText = attributedString
Note that there are a few different options available to you here, so you might need to find the right type of indent for your precise needs. If you right-click on NSMutableParagraphStyle and choose Jump To Definition you'll see all the options.
That's because a UILabel can have either plain text or attributed text. You set attributed text in the storyboard, and add some formatting (padding, etc.). Then, in code, you override that text (and all its formatting) with plain text.
If you want to keep the formatting, you need to set the label's attributedText property. And add all the necessary formatting to the NSAttributedString object you create for that.

NSAttributedString: letters interference with arabic font

I used arabic font called: "Hafs", it is from here:
http://fonts.qurancomplex.gov.sa/download/UthmanicHafs1Ver09Font.zip
I used it to draw Ayat from Holy Quran, but some letters are interfered with others, as shown in picture:
how to represent right text without interference?
thanks
EDIT:
here is another photo from "Pages" application to show you the original text, i coloured words that corrupted in iOS simulator by red colour
EDIT2:
I set attributed strings follows:
1- i paste the string into TextView in the storyboard.
2- i wrote this code in the related viewcontroller:
let font:UIFont! = UIFont(name: "KFGQPC Uthmanic Script HAFS", size: 30)
self.txt2.attributedText = NSAttributedString(string: txt2.text, attributes: [NSFontAttributeName:font])

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

Resources