Reputation: 1249
I need text with multiple tapeable links init. In Android I just used simple html string for that Example:
"<a href=\"https://www.link.com\">Photo</a> The owner / <a href=\"https://creativecommons.org/licenses/by-sa/3.0/deed.en\">CC BY-SA 3.0</a>"
And it worked only when I clicked on words corresponding links.
I spent a day and did not manage to find simple solution for ios. I tried to use extension for UITextView :
extension UITextView {
func setHTMLFromString(text: String) {
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%@</span>" as NSString, text)
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attrStr = try! NSAttributedString(
data: modifiedFont.data(using: String.Encoding.unicode.rawValue, allowLossyConversion: true)!,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.attributedText = attrStr
}
But links work not only when I click on the hyperlink world but also when I click on simple text between. And I did not manage to align this text in textView(center) as well.
Does anybody know correct way?
Upvotes: 0
Views: 75
Reputation: 77423
Can't find documentation at the moment, but close observation indicates:
Because tapping with a finger is not as precise as clicking with a mouse pointer, the link gets a larger "tap region" than the text itself.
With your example - "Photo The owner / CC BY-SA 3.0" - I can tap on the ow
characters without triggering either link. If my tap is on The
it triggers the Photo link, if my tap is on new /
it triggers that link.
Edit
One option to center the text...
In your extension, change
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%@</span>" as NSString, text)
to
let modifiedFont = NSString(format:"<p style=\"text-align:center\"><span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%@</span></p>" as NSString, text)
added the <p style=...
tag.
Upvotes: 1