user6032625
user6032625

Reputation: 197

How do I combine two attributed strings into one UILabel?

I have two attributed strings and I want to place them in one label, how would I do this?

let font = UIFont(name: "Hidden", size: 15)
    let addfont = UIFont(name: "Hidden", size: 15)
    var att = [NSFontAttributeName : font]
    let attrString = NSAttributedString(
        string: animalname[indexPath.row],
        attributes: NSDictionary(
            object: font!,
            forKey: NSFontAttributeName) as! [String : AnyObject])

    let attrStringAdd = NSAttributedString(
        string: animalloc[indexPath.row],
        attributes: NSDictionary(
            object: addfont!,
            forKey: NSFontAttributeName) as! [String : AnyObject])

    cell.animaltext.attributedText = attrString + attrStringAdd

Upvotes: 0

Views: 2010

Answers (1)

user3300864
user3300864

Reputation: 150

  1. Concatinated both the string.
  2. Find range for string1 and string2.
  3. Then for particular range of string apply desired attribute.
  4. Now assign this attributed string to UILabel.

Upvotes: 1

Related Questions