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

1
  • Can you clarify your question? eg: Post an image with the desired result Commented Sep 23, 2020 at 19:14

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.