2017-09-07 2 views
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! 
    } 

XIBファイル enter image description here

+0

投稿したスニペットがコンパイルされないため、コードを修正してください。 –

+0

numberOfLinesの値を0に変更すると、デフォルトはUILabelの1になります。 –

+0

@ Yogesh Sutharはい、それは0ですが、まだ動作していません。 – risa8

答えて

0

あなたはあなたのラベルの段落スタイルを設定しているし、その後は、バックプレーンテキストに設定します。注文を交換してください:

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() 
    } 
関連する問題