1

I having the Dynamic data, I load that data into tableview but these data having the mixing of Attributed and normal strings.

Here I getting the as "Tap here" ,I want to dilate that string with Blue colour and pick on that it need to open the url.

I write following code but it gives error.

Binary operator '+' cannot be applied to operands of type 'String' and '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
    }
Ben Rockey
  • 920
  • 6
  • 23
srikanth kumar
  • 77
  • 1
  • 10
  • please share code which you have written till now. – Taimoor Suleman May 29 '19 at 08:50
  • I am trying to fetch the tap in all cells there after I add here with different color – srikanth kumar May 29 '19 at 08:52
  • 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 } – srikanth kumar May 29 '19 at 08:52
  • You can prepare method, which takes dynamic string as parameter and returns the Attributted string as per your requirement, and you have to use that attributted string to cell.desclbl.text?.attributedString object – Bhavik Modi May 29 '19 at 08:54
  • 1
    Unrelated but do not use multiple arrays as data source. That's very bad practice and pretty error-prone. Use a custom struct or class. – vadian May 29 '19 at 09:01

1 Answers1

0

You can not append the NSAttributedString to String objects. I would suggest that you generate a normal String and then add the attributed parameters to it.

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
    }

This function will return a attributed string which you can use however you want. With tableview you can pass some parameters and modify the cell's attributes strings. I have added a google link behind the 'Tap Here' and its showing the output like below :

enter image description here

I hope I got your question right and this helps you in some way.

BhargavR
  • 1,095
  • 7
  • 14