[英]NSMutableAttributedString not working
I created a xib file with UIlabel. 我用UIlabel创建了一个xib文件。 I would like to add line spacing to UILabel.
我想向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. 该代码适用于其他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 // 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!
}
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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.