简体   繁体   中英

NSMutableAttributedString not working

I created a xib file with UIlabel. I would like to add line spacing to UILabel. I wrote the code below but it does not change the line spacing between lines. ( I tried with attribute editor and it didnt solve) What is the problem?

The code is working for other VC but not for this xib.

import UIKit

class TBLCell: UITableViewCell {
    static let id = "TBLCell"

    @IBOutlet var lbl:UILabel!
    @IBOutlet var view:UIView!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        let font = UIFont(name: "MuseoSansRounded-300", size: 18.0)!
        self.lbl.font = font
        self.lbl.numberOfLines = 0

    self.selectionStyle = .none


    }



    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    func topViewDesign() {
        self.lbl.textColor = UIColor.darkGray
        self.view.backgroundColor = UIColor.white
        self.view.layer.cornerRadius = 8.0
        self.view.layer.shadowOffset = CGSize.zero
        self.view.layer.shadowColor = UIColor.lightGray.withAlphaComponent(0.5).cgColor
        self.view.layer.shadowRadius = 2
        self.view.layer.shadowOpacity = 0.2
        self.view.layer.borderColor = UIColor(red: 217/255, green: 217/255, blue: 217/255, alpha: 1.0).cgColor
        self.view.layer.borderWidth = 1

            //add line spacing to lbl
        let attrString = NSMutableAttributedString(string: (self.lbl?.text)!)
        var style = NSMutableParagraphStyle()
        style.lineSpacing = 5
        attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: (self.lbl?.text?.characters.count)!))
        lbl.attributedText = attrString


    }

}

// cellForRowAtIndexpath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: TBLCell.id) as? TBLCell


        if cell == nil {
            cell = TBLCell(style: .default, reuseIdentifier: TBLCell.id)
        }

        let view = UIView()
        view.backgroundColor = .clear
        cell?.backgroundView = view
        cell?.backgroundColor = .clear

        let rows = self.sections[indexPath.section]
        if indexPath.section == 0 {
            cell?.topViewDesign()
            //Question Row
            let question = rows[0]
            cell?.lbl.text = question
        }else if indexPath.section == 1 {
            //Translation Row
            cell?.bottomViewDesign()
            let question = rows[0]
            cell?.lbl.text = question
        }
        return cell!
    }

Xib file 在此处输入图片说明

You are setting the paragraph style of your label, and then setting it back to plain text. Swap the order:

    let question = rows[0]

    // set the text for the label
    cell?.lbl.text = question

    // *then* set the style
    if indexPath.section == 0 {
        //Question Row
        cell?.topViewDesign()
    }else if indexPath.section == 1 {
        //Translation Row
        cell?.bottomViewDesign()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM