0
UIlabelでxibファイルを作成しました。私はUILabelに行間隔を追加したいと思います。私は以下のコードを書いたが、行間の行間隔は変更しない。 (私は属性エディタで試したが解決しなかった) 何が問題なの?NSMutableAttributedStringが機能しない
このコードは他の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!
}
投稿したスニペットがコンパイルされないため、コードを修正してください。 –
numberOfLinesの値を0に変更すると、デフォルトはUILabelの1になります。 –
@ Yogesh Sutharはい、それは0ですが、まだ動作していません。 – risa8