简体   繁体   中英

swift: Set the hyperlink on the Text

I am trying to set Hyperlink in the text. Text appears but i cannot find the link on the text. Can anyone help here. I want to set the link on My Health Action Plan on getStartedDict. but only whole text appears without any links.

let link1 = ["My Health Action Plan"]
var getStartedDict =
   ["":"Now that you have a glimpse of where you currently are in managing your health, it is time for you to update your Health Action Plan. Use the information in the Health Profile along with the online resources in this portal to help set realistic health goals and to help you work toward better health."]
var linkArray = [[String]]()

override func viewDidLoad(){
    super.viewDidLoad()
    linkArray.removeAll()
    linkArray.append(link1)
    CommonUtility.sharedInstance.addBackButton(self)
    loadData()
 }

//here i tried to set the link

  if let getStartedText = self.getStartedDict[self.model.label1]?.htmlStringToAttributedString()
        {
            let aText = getStartedText
            let myRange = NSRange(location: 0, length: aText.length)
            aText.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(kFontSize), range: myRange)
            print(aText)
            cell.detail.setLinksForSubstrings(linkArray, withLinkHandler: handler)

        }

Updated: Refer to comment for answer

finally after long hours of Headache i have done this:

var getStartedDict =
   ["Value":"Now that you have a glimpse of where you currently are in managing your health, it is time for you to update your Health Action Plan. Use the information in the Health Profile along with the online resources in this portal to help set realistic health goals and to help you work toward better health."]

 override func viewDidLoad(){
        super.viewDidLoad()
        CommonUtility.sharedInstance.addBackButton(self)
        loadData()
     }

This is how we can set the link

if let getText = self.getStartedDict["Value"]?.htmlStringToAttributedString(){
        let aText = getText
        let myRange = NSRange(location: 0, length: aText.length)
        aText.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(kFontSize), range: myRange)
        cell.detail.attributedText = aText
        cell.detail.setLinksForSubstrings(["Health Action Plan"], withLinkHandler: handler)
    }
    else{
        print("No text found")
    }

Fell free to comment in case, if there is a better way for this.

我认为这个库对您更有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM