【问题标题】:Add Nsattributed string and string in swift在swift中添加Nsattributed字符串和字符串
【发布时间】:2019-05-29 08:49:37
【问题描述】:

我拥有动态数据,我将该数据加载到 tableview 中,但这些数据混合了属性字符串和普通字符串。

在这里我得到“点击这里”,我想用蓝色扩大那个字符串并选择它需要打开网址。

我写了以下代码,但它给出了错误。

二元运算符“+”不能应用于“字符串”类型的操作数和 'NSMutableAttributedString'

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "WhatisrewardsTableViewCell", for: indexPath)as! WhatisrewardsTableViewCell
        cell.imageview.image = images[indexPath.section][indexPath.row]
        cell.titlelbl.text = tilenames[indexPath.section][indexPath.row]
        cell.desclbl.text = descriptions[indexPath.section][indexPath.row]

        let value = " here.".withTextColor(UIColor.disSatifyclr)

        if (cell.desclbl.text?.contains("tap"))!{
           // cell.desclbl.text?.attributedString
            cell.desclbl.text = descriptions[indexPath.section][indexPath.row] + value
        }

        cell.desclbl.addLineSpacing()
        return cell
    }

【问题讨论】:

  • 请分享你写到现在的代码。
  • 在此处添加不同颜色后,我正在尝试获取所有单元格中的水龙头
  • let value = " here.".withTextColor(UIColor.disSatifyclr) if (cell.desclbl.text?.contains("tap"))!{ // cell.desclbl.text?.attributedString cell.desclbl.text = 描述[indexPath.section][indexPath.row] + 值}
  • 您可以准备方法,该方法以动态字符串为参数并根据您的要求返回属性字符串,您必须使用该属性字符串到 cell.desclbl.text?.attributedString 对象
  • 不相关但不使用多个数组作为数据源。这是非常糟糕的做法,而且很容易出错。使用自定义结构或类。

标签: ios swift tableview nsattributedstring


【解决方案1】:

您不能将 NSAttributedString 附加到 String 对象。我建议你生成一个普通的字符串,然后将属性参数添加到它。

func prepareURLString() -> NSMutableAttributedString {

        let masterString = "To navigate to google Tap Here."

        let formattedString = NSMutableAttributedString(string: masterString)

        let formattedStringAttribute = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular),
            NSAttributedString.Key.foregroundColor: UIColor(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1),
            ] as [NSAttributedString.Key : Any]

        let privacyPolicyAttribute = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .bold),
            NSAttributedString.Key.foregroundColor: UIColor.blue,
            NSAttributedString.Key.underlineStyle: 1,
            NSAttributedString.Key.link: URL(string: "https://www.google.com")!
            ] as [NSAttributedString.Key : Any]

        formattedString.addAttributes(formattedStringAttribute, range: NSMakeRange(0, formattedString.length))

        let privacyPolicyRange = (masterString as NSString).range(of: "Tap Here")
        formattedString.addAttributes(privacyPolicyAttribute, range: privacyPolicyRange)

        return formattedString
    }

此函数将返回一个属性字符串,您可以随意使用它。使用 tableview,您可以传递一些参数并修改单元格的属性字符串。我在“点击此处”后面添加了一个谷歌链接,并显示如下输出:

我希望你的问题是正确的,这对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多