OGeek|极客世界-中国程序员成长平台

标题: ios - swift:在文本上设置超链接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:33
标题: ios - swift:在文本上设置超链接

我正在尝试在文本中设置超链接。文本出现,但我在文本上找不到链接。任何人都可以在这里帮忙。 我想在 getStartedDict 上设置 我的健康行动计划 的链接。但只显示整个文本,没有任何链接。

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()
 }

//这里我尝试设置链接

  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)

        }

更新:请参阅评论以获取答案



Best Answer-推荐答案


经过长时间的头痛后,我终于做到了:

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()
     }

我们可以这样设置链接

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")
    }

如果有更好的方法,请随时发表评论。

关于ios - swift:在文本上设置超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41584753/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4