NSForegroundColorAttributeName for UILabel using NSAttributedString - ios

I have NSAttributed string with links in it and I want to load it inside UILabel. I works fine, however all links are blue Color.
let string = NSMutableAttributedString(attributedString: attributedText)
string.addAttributes([NSForegroundColorAttributeName:linkColor], range: linkRange)
self.attributedText = string
No change to foreground color, setting all other attributes work, like strikethrough style. Just link always stays blue.
NSAttributed string is generated from HTML if that makes any difference.

Ended up doing
class AttributedTextLabel:UILabel {
var attributedString:NSAttributedString?{
didSet{
guard let attributedString = attributedString else {
return
}
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
mutableAttributedString.enumerateAttribute(NSLinkAttributeName, inRange: NSRange(location: 0, length: attributedString.length), options: NSAttributedStringEnumerationOptions.Reverse) {[weak self] (attribute, range, other) in
if let url = attribute as? NSURL {
mutableAttributedString.removeAttribute(NSLinkAttributeName, range: range)
self?.links.append(Link(url: url, range: range))
}
}
self.attributedText = mutableAttributedString
}
}
struct Link {
var url:NSURL
var range:NSRange
}
var links:[Link] = []
var edgeInsets:UIEdgeInsets = UIEdgeInsetsZero
private var textContentSize:CGSize {
let textContainerWidth = frame.width - edgeInsets.left - edgeInsets.right
let textContainerHeight = frame.height - edgeInsets.top - edgeInsets.bottom
return CGSizeMake(textContainerWidth, textContainerHeight)
}
func characterIndexAtPoint(point:CGPoint) -> Int? {
guard let attributedText = attributedText else {
return nil
}
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: textContentSize)
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = self.lineBreakMode
textContainer.maximumNumberOfLines = self.numberOfLines
layoutManager.addTextContainer(textContainer)
let storage = NSTextStorage(attributedString: attributedText)
storage.addLayoutManager(layoutManager)
let adjustedPoint = CGPointMake(point.x-edgeInsets.left, point.y-edgeInsets.top)
let characterIndex = layoutManager.characterIndexForPoint(point, inTextContainer: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return characterIndex
}
override func drawTextInRect(rect: CGRect) {
return super.drawTextInRect(UIEdgeInsetsInsetRect(rect, edgeInsets))
}
private var selectedRange:NSRange?
private var highligtedLink:Link? {
didSet{
let string = self.attributedText as! NSMutableAttributedString
if let oldValue = oldValue {
if let selectedLinkColor = NativeTextKit.TextAttributes.selectedLinkColor.value {
string.addAttributes([
NSForegroundColorAttributeName:selectedLinkColor
], range: oldValue.range)
}
}
if let highligtedLink = highligtedLink {
if let selectedLinkColor = NativeTextKit.TextAttributes.selectedLinkColor.value {
string.addAttributes([
NSForegroundColorAttributeName:selectedLinkColor
], range: highligtedLink.range)
}
}
self.attributedText = string
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else {
return
}
let char = characterIndexAtPoint(touch.locationInView(self))
let string = self.attributedText as! NSMutableAttributedString
highligtedLink = linkForTouch(touch)
string.addAttributes([
NSForegroundColorAttributeName:UIColor.brownColor()
], range: NSMakeRange(char!, 1))
attributedText = string
}
func linkForTouch(touch:UITouch)->Link? {
guard let attributedText = attributedText else {
return nil
}
guard let characterIndex = characterIndexAtPoint(touch.locationInView(self)) else {
return nil
}
return links.filter({NSLocationInRange(characterIndex, $0.range)}).first
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
highligtedLink = nil
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else {
return
}
if let highligtedLink = highligtedLink, let lastTouchedLink = linkForTouch(touch) where highligtedLink.url == lastTouchedLink.url {
urlInteractionHandler?(textView: UITextView(), url:lastTouchedLink.url)
}
}
/// Executed on link interaction
var urlInteractionHandler:URLInteractionHandler?
}
Does the job, took a while to figure out. Because UILabel has its own link formatting ended up
Remove all links from attributed string once string is set
Add links and ranges to array
After link is selected use NSTextContainer to figure out what index the character was
Find range that character belongs to
Return link

Related

Is there a way to track if the character that user inputed is between two other character in swift?

I'm developing an App, which has this feature: when user press a shortcut button, the UITextView is entered into the corresponding symbol (like "**", "__" or """"). Then the cursor turn back to the position between the pair of symbols, and user input what the want.
When user has done, he can press the "enter" button to jump out of the pair of symbol and input sth else.
But if the user change the cursor manually, for example he move the cursor out of the symbols.He press enter then display a normal line feed.
I have mostly done this function. But I don't know how to track if the cursor is still between the pair of symbols.
#objc func shortcutFunc(sender: CustomBtn, forEvent: UIEvent) {
insertFromCursor(sender: sender, forEvent: forEvent)
retreat = sender.retreat!
var selectedView: CustomTextView?
if bodyViewUnderEditing {
selectedView = textField.bodyView
} else if titleViewUnderEditing {
selectedView = textField.titleView
}
if let selectedView = selectedView {
let location = selectedView.selectedRange.location
selectedView.selectedRange = NSRange(location: location - sender.retreat!, length: 0)
isShortcutBtnInputing = true
}
}
#objc func insertFromCursor(sender: CustomBtn, forEvent event: UIEvent) {
var selectedView: CustomTextView?
if bodyViewUnderEditing {
selectedView = textField.bodyView
} else if titleViewUnderEditing {
selectedView = textField.titleView
}
if let selectedView = selectedView {
let range = selectedView.selectedRange
let start = selectedView.position(from: selectedView.beginningOfDocument, offset: range.location)!
let end = selectedView.position(from: start, offset: range.length)!
let textRange = selectedView.textRange(from: start, to: end)!
selectedView.replace(textRange, withText: sender.argument!)
}
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if textView == textField.bodyView {
if text == "\n", isShortcutBtnInputing, bodyViewUnderEditing {
let location = textField.bodyView.selectedRange.location
textField.bodyView.selectedRange = NSRange(location: location + retreat, length: 0)
isShortcutBtnInputing = false
return false
}
return true
} else if textView == textField.titleView {
if text == "\n" {
if isShortcutBtnInputing, titleViewUnderEditing {
let location = textField.titleView.selectedRange.location
textField.titleView.selectedRange = NSRange(location: location + retreat, length: 0)
isShortcutBtnInputing = false
} else {
textField.bodyView.becomeFirstResponder()
}
return false
}
return true
}
return true
}

Get string from substring

I have following string "#[Hema](hema_ramburuth), #[Ilesh P](ilesh.panchal), #[Lewis Murphy](lewis) how are you?". I want to display this screen like this "Hema, Ilesh P, Lewis Murphy how are you?" also I want to identify the screen for the click event.
I have used the ActiveLabel repo for the click.
Hey I have had encountered a similar requirement. So this is how I have handled.
I have created an extension for String
extension String {
/// Returns range of text in the string
func getRange(OfText text: String) -> NSRange {
let nsRepresentation = self as NSString
return nsRepresentation.range(of: text)
}
}
In your View Controller,
var tapPrivacyGesture = UITapGestureRecognizer()
#IBOutlet weak var yourLabel: UILabel!
var displayText = String()
func matchesForRegexInText(regex: String, text: String, firstBracket: String, lastBracket: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matches(
in: text,
options: [],
range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range) }.map { $0.replacingOccurrences(of: firstBracket, with: "") }.map { $0.replacingOccurrences(of: lastBracket, with: "") }
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
var givenString = "#[Hema](hema_ramburuth), #[Ilesh P](ilesh.panchal), #[Lewis Murphy](lewis) how are you?"
let nameStrings = matchesForRegexInText(regex: "\\[(.*?)\\]", text: givenString, firstBracket: "[", lastBracket: "]")
let removeForUIStrings = matchesForRegexInText(regex: "\\((.*?)\\)", text: givenString, firstBracket: "(", lastBracket: ")")
removeForUIStrings.forEach {
givenString = givenString.replacingOccurrences(of: "(\($0))", with: "")
}
nameStrings.forEach {
givenString = givenString.replacingOccurrences(of: "[\($0)]", with: $0)
}
givenString = givenString.replacingOccurrences(of: "#", with: "")
print(givenString)
displayText = givenString
tapPrivacyGesture.addTarget(self, action: #selector(self.handlePolicyTap(tap:)))
yourLabel.addGestureRecognizer(tapPrivacyGesture)
yourLabel.isUserInteractionEnabled = true
func handlePolicyTap(tap: UITapGestureRecognizer) {
let storage = NSTextStorage(attributedString: yourLabel.attributedText ?? NSAttributedString())
let layoutManager = NSLayoutManager()
storage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: CGSize(width: yourLabel.frame.size.width, height: yourLabel.frame.size.height+100))
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = (yourLabel.lineBreakMode)
textContainer.maximumNumberOfLines = yourLabel.numberOfLines
layoutManager.addTextContainer(textContainer)
let location: CGPoint = tap.location(in: yourLabel)
let characterIndex: Int = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
guard
characterIndex < storage.length,
let question = currentQuestion else {
return
}
nameStrings.forEach {
let range = displayText.getRange(OfText: $0)
if range.contains(characterIndex) {
/// Perform actions on click of this string
}
}
}
As from your question, just hard-code parsing done below.
let fullString = "#[Hema](hema_ramburuth), #[Ilesh P](ilesh.panchal), #[Lewis Murphy](lewis) how are you?"
let allarray = fullString.split(separator: ",")
let messageArray = allarray.last
let message = messageArray?.split(separator: ")")
let correctMessage = message?.last
var allNames : String = ""
for namesString in allarray {
if allNames.count > 0 {
allNames += ", "
}
let name = String(namesString)
allNames += name.slice(from: "#[", to: "]") ?? ""
}
if allNames.count > 0 {
allNames += correctMessage ?? ""
}
print("Name and Message --- > \(allNames)")
Slicing string using String extension
extension String {
func slice(from: String, to: String) -> String? {
return (range(of: from)?.upperBound).flatMap { substringFrom in
(range(of: to, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
substring(with: substringFrom..<substringTo)
}
}
}
}
I've printed output as below:
Name and Message --- > Hema, Ilesh P, Lewis Murphy how are you?

NSAttributedString changing the font size

I am using this extension of String to get proper attributed text from the HTML tags in a String.
extension String {
var html2AttributedString: NSAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding ], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
And I am using the value in a UILabel inside a UICollectionView.
if let value = mainNote.html2AttributedString
{
cell.note.attributedText = value
}
else
{
cell.note.text = mainNote
}
This method works great. But by default it comes with the "Times New Roman" font with size of 11. So I wanted to make it little bigger. I tried using NSMutableAttributedString.
if let value = mainNote.html2AttributedString
{
let mutableString = NSMutableAttributedString(string: value.string, attributes: [NSFontAttributeName : UIFont(name: "Times New Roman", size: 20)!])
cell.note.attributedText = mutableString
}
else
{
cell.note.text = mainNote
}
Which actually did nothing.
If I increase the font size of UILabel directly then the font size gets increased but the italic attribute does not work.
cell.note.font = UIFont(name: "Times New Roman", size: 16)
Please help me out here to make the String little bigger.
Use this updated extension:
extension String {
func html2AttributedString(font: UIFont?) -> NSAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
let string = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)
let newString = NSMutableAttributedString(attributedString: string)
string.enumerateAttributesInRange(NSRange.init(location: 0, length: string.length), options: .Reverse) { (attributes : [String : AnyObject], range:NSRange, _) -> Void in
if let font = font {
newString.removeAttribute(NSFontAttributeName, range: range)
newString.addAttribute(NSFontAttributeName, value: font, range: range)
}
}
return newString
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString(nil)?.string ?? ""
}
}
Usage:
if let value = mainNote.html2AttributedString(UIFont(name: "Times New Roman-ItalicMT", size: 20))
{
cell.note.attributedText = value
}
else
{
cell.note.text = mainNote
}

How to display clickable links in UITextView

I am trying to display an attributed string in a UITextview with clickable links. I've created a simple test project to see where I'm going wrong and still can't figure it out. I've tried enabling user interaction and setting the shouldInteractWithURLs delegate method, but it's still not working. Here's my code (for a view controller that only contains a textview)
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let string = "Google"
let linkString = NSMutableAttributedString(string: string)
linkString.addAttribute(NSLinkAttributeName, value: NSURL(string: "https://www.google.com")!, range: NSMakeRange(0, string.characters.count))
linkString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue", size: 25.0)!, range: NSMakeRange(0, string.characters.count))
textView.attributedText = linkString
textView.delegate = self
textView.selectable = true
textView.userInteractionEnabled = true
}
And here are the delegate methods I've implemented:
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
return false
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
return true
}
This still isn't working. I've searched on this topic and nothing has helped yet. Thanks so much in advance.
Just select the UITextView in your storyboard and go to "Show Attributes inspector" and select selectable and links. As the image below shows. Make sure Editable is unchecked.
For swift3.0
override func viewDidLoad() {
super.viewDidLoad()
let linkAttributes = [
NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
] as [String : Any]
let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
textview.delegate = self
textview.attributedText = attributedString
textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
textview.textColor = UIColor.white
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}
Swift 3 iOS 10: Here's Clickable extended UITextView that detect websites inside the textview automatically as long as the link start with www. for example: www.exmaple.com if it exist anywhere in the text will be clickable. Here's the class:
import Foundation
import UIKit
public class ClickableTextView:UITextView{
var tap:UITapGestureRecognizer!
override public init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
print("init")
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
// Add tap gesture recognizer to Text View
tap = UITapGestureRecognizer(target: self, action: #selector(self.myMethodToHandleTap(sender:)))
// tap.delegate = self
self.addGestureRecognizer(tap)
}
func myMethodToHandleTap(sender: UITapGestureRecognizer){
let myTextView = sender.view as! UITextView
let layoutManager = myTextView.layoutManager
// location of tap in myTextView coordinates and taking the inset into account
var location = sender.location(in: myTextView)
location.x -= myTextView.textContainerInset.left;
location.y -= myTextView.textContainerInset.top;
// character index at tap location
let characterIndex = layoutManager.characterIndex(for: location, in: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
// if index is valid then do something.
if characterIndex < myTextView.textStorage.length {
let orgString = myTextView.attributedText.string
//Find the WWW
var didFind = false
var count:Int = characterIndex
while(count > 2 && didFind == false){
let myRange = NSRange(location: count-1, length: 2)
let substring = (orgString as NSString).substring(with: myRange)
// print(substring,count)
if substring == " w" || (substring == "w." && count == 3){
didFind = true
// print("Did find",count)
var count2 = count
while(count2 < orgString.characters.count){
let myRange = NSRange(location: count2 - 1, length: 2)
let substring = (orgString as NSString).substring(with: myRange)
// print("Did 2",count2,substring)
count2 += 1
//If it was at the end of textView
if count2 == orgString.characters.count {
let length = orgString.characters.count - count
let myRange = NSRange(location: count, length: length)
let substring = (orgString as NSString).substring(with: myRange)
openLink(link: substring)
print("It's a Link",substring)
return
}
//If it's in the middle
if substring.hasSuffix(" "){
let length = count2 - count
let myRange = NSRange(location: count, length: length)
let substring = (orgString as NSString).substring(with: myRange)
openLink(link: substring)
print("It's a Link",substring)
return
}
}
return
}
if substring.hasPrefix(" "){
print("Not a link")
return
}
count -= 1
}
}
}
func openLink(link:String){
if let checkURL = URL(string: "http://\(link.replacingOccurrences(of: " ", with: ""))") {
if UIApplication.shared.canOpenURL(checkURL) {
UIApplication.shared.open(checkURL, options: [:], completionHandler: nil)
print("url successfully opened")
}
} else {
print("invalid url")
}
}
public override func didMoveToWindow() {
if self.window == nil{
self.removeGestureRecognizer(tap)
print("ClickableTextView View removed from")
}
}
}

iOS: Find url in string and change the string to be clickable (tapable)

I have a string (for example: "This is some text with specific http://goo.gl/45hz web adress in it").
I need to find all URLs within the string if any and then convert somehow the string so the address will be tapable (safari will open if user taps on it) and i need to display the whole string with URL in label idealy (if possible).
Does anybody know how can be this achieved?
In order to find all URLS in a string
NSError *error = NULL;
NSString *string = #"This is some text with specific http://goo.gl/45hz web adress in it";
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
NSArray *matches = [detector matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeLink) {
NSURL *url = [match URL];
NSLog(#"url...%#", url);
}
}
You can use NSRange to find the http:// first and and from that location, discard the text before http://, finally you can separate the remaining string using spaces, take first part of the remaining string which will contain URL.
NSString *givenStr = #"This is some text with specific http://goo.gl/45hz web address in it";
NSRange range = [givenStr rangeOfString:#"http://" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
NSString *urlString = [givenStr substringFromIndex:range.location];
// urlString="http://goo.gl/45hz web address in it"
NSArray *urlStrArray = [urlString componentsSeparatedByString:#" "];
NSURL *finalURL=[NSURL URLWithString:[urlStrArray objectAtIndex:0]];
// [urlStrArray objectAtIndex:0]="http://goog.gl/45hz"
}
For making URL clickable as said by #calampunay you should use UITextView instead of UILabel because UILabel presents only plain text.
You can use a UITextView instead of UILabel. Set editable to NO and then modify the property #property(nonatomic) UIDataDetectorTypes dataDetectorTypes to detect URLs.
#In swift 5 you will try like this way to find URL
let input = "here is your String"
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range:NSRange(location: 0, length: input.utf16.count))
for match in matches {
guard let range = Range(match.range, in: input) else { continue }
let url = input[range]
print(url)
}
Use TTTAttributedLabel if this is coming from a UI label - https://github.com/mattt/TTTAttributedLabel
Works great for us.
I have implemented same feature in swift 3. Below is the complete code to detect url in string and make it tapable :
//put these lines of code inside your function
//add Tap Gesture
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapLabel))
myLabel?.addGestureRecognizer(tapGesture)
let input = “tap on url http://www.google.com, to go to google search”
myLabel?.attributedText = getAttributedString(input: input);
/*** functions to perform complete task ***/
//function to get attributed string with url
func getAttributedString(input : String) -> NSMutableAttributedString {
let matches = getURLRange(input: input)
let attributedString = NSMutableAttributedString(string:input)
for match in matches {
let url = (input as NSString).substring(with: match.range)
print(url)
let linkText = NSMutableAttributedString(string:url, attributes:[NSForegroundColorAttributeName : UIColor.blue] as [String : Any])
attributedString.replaceCharacters(in: match.range, with: linkText)
}
return attributedString
}
//function to get urls range
func getURLRange(input : String) -> [NSTextCheckingResult] {
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
return matches
}
// function to check if URL is taped
func tapLabel(gesture: UITapGestureRecognizer) {
let matches = getURLRange(input: (myLabel?.text)!)
let tapLocation = gesture.location(in: myLabel)
let indexOfCharacter = didTapAttributedTextInLabel(label: myLabel!, tapLocation: tapLocation)
for match in matches {
if NSLocationInRange(indexOfCharacter, match.range) {
//open selected URL
let mainText = myLabel?.text as NSString?
let urlToOpen = URL(string: (mainText?.substring(with: match.range))!)
UIApplication.shared.open(urlToOpen!)
break;
}else {
print("Tapped none")
}
}
}
//function to get index Of selected Character in string
func didTapAttributedTextInLabel(label: UILabel, tapLocation: CGPoint) -> Int {
//here myLabel is the object of UILabel
//added this from #warly's answer
//set font of attributedText
let attributedText = NSMutableAttributedString(attributedString: myLabel!.attributedText!)
attributedText.addAttributes([NSFontAttributeName: myLabel!.font], range: NSMakeRange(0, (myLabel!.attributedText?.string.characters.count)!))
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize(width: (myLabel?.frame.width)!, height: (myLabel?.frame.height)!+100))
let textStorage = NSTextStorage(attributedString: attributedText)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
// Configure textContainer
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = myLabel!.lineBreakMode
textContainer.maximumNumberOfLines = myLabel!.numberOfLines
let labelSize = myLabel!.bounds.size
textContainer.size = labelSize
// get the index of character where user tapped
let indexOfCharacter = layoutManager.characterIndex(for: tapLocation, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return indexOfCharacter
}
It is tested and working fine inside my app.
Swift 4 implementation of Ankur's answer:
let messageLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
label.isUserInteractionEnabled = true
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(messageLabel)
// setup messageLabel constraints
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapLabel))
messageLabel.addGestureRecognizer(tapGesture)
}
//function to get attributed string with url
func getAttributedString(input : String) -> NSMutableAttributedString {
let matches = getURLRange(input: input)
let attributedString = NSMutableAttributedString(string:input)
for match in matches {
let url = (input as NSString).substring(with: match.range)
let linkText = NSMutableAttributedString(string:url, attributes:[NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue) : UIColor.appleBlue()])
attributedString.replaceCharacters(in: match.range, with: linkText)
}
return attributedString
}
func getURLRange(input : String) -> [NSTextCheckingResult] {
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
return matches
}
//function to get index Of selected Character in string
func didTapAttributedTextInLabel(label: UILabel, tapLocation: CGPoint) -> Int {
//here myLabel is the object of UILabel
//added this from #warly's answer
//set font of attributedText
let attributedText = NSMutableAttributedString(attributedString: messageLabel.attributedText!)
attributedText.addAttributes([NSAttributedString.Key.font: messageLabel.font], range: NSMakeRange(0, (messageLabel.attributedText?.string.count)!))
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize(width: messageLabel.frame.width, height: messageLabel.frame.height+100))
let textStorage = NSTextStorage(attributedString: attributedText)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
// Configure textContainer
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = messageLabel.lineBreakMode
textContainer.maximumNumberOfLines = messageLabel.numberOfLines
let labelSize = messageLabel.bounds.size
textContainer.size = labelSize
// get the index of character where user tapped
let indexOfCharacter = layoutManager.characterIndex(for: tapLocation, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return indexOfCharacter
}
// function to check if URL is taped
#objc func tapLabel(gesture: UITapGestureRecognizer) {
let matches = getURLRange(input: (messageLabel.text)!)
let tapLocation = gesture.location(in: messageLabel)
let indexOfCharacter = didTapAttributedTextInLabel(label: messageLabel, tapLocation: tapLocation)
for match in matches {
if NSLocationInRange(indexOfCharacter, match.range) {
//open selected URL
let mainText = messageLabel.text as NSString?
if let urlToOpen = URL(string: (mainText?.substring(with: match.range))!) {
UIApplication.shared.open(urlToOpen)
} else {
print("We have error with URL")
}
break
} else {
print("Tapped none")
}
}
}
Swift 5 extension
extension String {
func extractUrl() -> URL? {
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count))
for match in matches {
guard let range = Range(match.range, in: self) else { continue }
let url = self[range]
return URL(string: String(url))
}
return nil
}
}

Resources