swift UILabel 文字调整行间距 的方法

时间:2023-02-01 08:34:15

let str_ss ="请认真填写银行帐户信息,如果填写不正确,将会影响到您的资金到帐时间,一旦填写不能随意变更,如需变更需提交申请,客服人员会在7个工作日内完成审核."

        let desc_str_lab:UILabel = UILabel(frame: CGRect(x:20,y: addH-32/4.6+300,width:SCREEN_WIDTH-40,height:100))

        desc_str_lab.text = str_ss

        desc_str_lab.textAlignment = NSTextAlignment.left

        desc_str_lab.numberOfLines =0//重要

        desc_str_lab.textColor = UIColor.red

        desc_str_lab.lineBreakMode = NSLineBreakMode.byWordWrapping//重要

        desc_str_lab.font = UIFont(name: "HelveticaNeue", size:14)

        

        self.scrollView!.addSubview(desc_str_lab)

        

        let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: str_ss)

        let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle()

        paragraphStyle.lineSpacing =7//大小调整

        attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, str_ss.characters.count))

        

        desc_str_lab.attributedText = attributedString

        desc_str_lab.sizeToFit()