gpt4 book ai didi

ios - NSMutableAttributedString 不起作用

转载 作者:行者123 更新时间:2023-11-30 12:10:43 26 4
gpt4 key购买 nike

我使用 UIlabel 创建了一个 xib 文件。我想为 UILabel 添加行距。我编写了下面的代码,但它不会改变行之间的行间距。 (我尝试使用属性编辑器,但没有解决)有什么问题吗?

该代码适用于其他 VC,但不适用于此 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 文件 enter image description here

最佳答案

您正在设置标签的段落样式,然后将其设置回纯文本。交换顺序:

    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()
}

关于ios - NSMutableAttributedString 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46098219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com