0

I am looking for align Text in NSAttributedString. It should be on same indent(List of string on same Column).

Output:

enter image description here

I have used below code

    func setAttributedText() {
   
        let image1Attachment = NSTextAttachment()
        image1Attachment.image = UIImage(named: "checkgreen.png")
        image1Attachment.bounds = CGRect(x: 0,
                                         y: (contentLabel.font.capHeight - image1Attachment.image!.size.height).rounded() / 2, width: image1Attachment.image!.size.width,
                                         height: image1Attachment.image!.size.height)
        
        var attributes = [NSAttributedString.Key: Any]()
        attributes[.font] = UIFont.preferredFont(forTextStyle: .body)
        attributes[.foregroundColor] = UIColor.black
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.headIndent = 50
        attributes[.paragraphStyle] = paragraphStyle
        
        
        
        let line0 = NSAttributedString(string: "Dummy text is text that is used in the publishing industry or by web designers to occupy the space which will later be filled with 'real' content. \n")
        let line1 = NSAttributedString(string: "Your account will be charged for renewal within 24-hours prior to the end of the current subscription perio\n")
//        let line2 = NSAttributedString(string: "You can manage your subscriptions and turn off auto-renewal by going to your Account Settings on th")

            
        
        let checkgreenImageAttribute = NSMutableAttributedString(attributedString: NSAttributedString(attachment: image1Attachment))
        let finalString = NSMutableAttributedString(attributedString: checkgreenImageAttribute)
        finalString.append(line0)
        finalString.append(checkgreenImageAttribute)
        finalString.append(line1)
//        finalString.append(checkgreenImageAttribute)
//        finalString.append(line2)
        contentLabel.attributedText = finalString
    }

NOTE: i don't want to use bullet points

Amit
  • 556
  • 1
  • 7
  • 24

1 Answers1

0

In an attributed string, a NSTextAttachment becomes a character in the string.

So, apply your attributes to the entire string after you've "assembled" it:

    let checkgreenImageAttribute = NSMutableAttributedString(attributedString: NSAttributedString(attachment: image1Attachment))
    let finalString = NSMutableAttributedString(attributedString: checkgreenImageAttribute)
    finalString.append(line0)
    finalString.append(checkgreenImageAttribute)
    finalString.append(line1)
    
    // apply paragraph attributes here
    finalString.addAttributes(attributes, range: NSRange(location: 0, length: finalString.length))
DonMag
  • 69,424
  • 5
  • 50
  • 86