【问题标题】:swift: Set the hyperlink on the Textswift:在文本上设置超链接
【发布时间】:2017-05-25 21:10:48
【问题描述】:

我正在尝试在文本中设置超链接。文本出现,但我在文本上找不到链接。任何人都可以在这里帮忙。 我想在 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)

        }

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

【问题讨论】:

  • 是的,我已经看过了,但确实成功了..你能帮帮我吗
  • 你也可以通过这种方法帮助我
  • 使用属性字符串将您的文本作为超链接
  • 能否发表评论让我了解更多

标签: ios swift hyperlink


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多