0

Want to add left margin spacing with NSMutableAttributedString NSMutableParagraphStyle & firstLineHeadIndent but no luck, firstLineHeadIndent or headIndent does't take effect

my implementation:

lazy var priseField: UILabel = {
     let label = UILabel()

     label.font = UIFont.boldSystemFont(ofSize: 15)
     label.textColor = .black
     let string = self.addMutableAttribute(string: "50")
     string.append(self.addAttribute(string: "lari"))
     label.attributedText = string
     return label
}()

func addMutableAttribute(string:String = "") -> NSMutableAttributedString {

    let attribute:[NSAttributedString.Key : Any] = [ NSAttributedString.Key.foregroundColor: UIColor.black,
                                                     NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15)]

    let attrString = NSMutableAttributedString(string: string, attributes: attribute)
    return attrString
}

func addAttribute(string:String = "") -> NSMutableAttributedString {

    let paragraphStyle = NSMutableParagraphStyle()

    paragraphStyle.firstLineHeadIndent = 3;
    paragraphStyle.headIndent = 3

    let attribute:[NSAttributedString.Key : Any] = [

        .foregroundColor: UIColor.darkGray,
        .paragraphStyle: paragraphStyle,
        .font: UIFont.boldSystemFont(ofSize: 12),
        .baselineOffset: 1
    ]

    let attrString = NSMutableAttributedString(string: string, attributes: attribute)
    return attrString
}

and result is:

enter image description here

I want to improve space between 50 and lari

Hattori Hanzō
  • 2,349
  • 4
  • 19
  • 36
  • 1
    I think that the problem is the fact that there is no paragraph start. I usually prefer to add a manual (non-breaking) space. You can also change kerning for that space to change its width. – Sulthan Feb 13 '19 at 14:05
  • 1
    "headIndent" works only at the start of the line not in the middle of it. Try "paragraphSpacingBefore". – Klinki Feb 13 '19 at 14:28

0 Answers0