2017-09-07 1 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 중 하나입니다. –

+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() 
    } 
관련 문제