2017-01-20 2 views
-2

내부에 단일 레이블이 있고 이상한 셀이있는 테이블보기가 있습니다. 나는이 하나의 라벨은 2 개 가지 색상으로 텍스트를 포함합니다 :UITableViewCell - 속성이있는 텍스트가있는 UILabel

let red = NSAttributedString(string: "Red color", 
         attributes: 
           [NSForegroundColorAttributeName: UIColor.redColor(), 
              NSFontAttributeName: normalFont]) 
let blue = NSAttributedString(string: "Blue color", 
          attributes: 
           [NSForegroundColorAttributeName: UIColor.blueColor(), 
              NSFontAttributeName: boldFont]) 

let attributedText = NSMutableAttributedString(attributedString: red) 
attributedText.appendAttributedString(blue) 

self.labelInformation.attributedText = attributedText 

문제 : 텍스트가 모두 파란색 나는 이유를 이해할 수 없다. 내가 attributedText을 인쇄 할 경우

내가 얻을 :

Red color 
{ 
    NSColor = "UIExtendedSRGBColorSpace 1 0 0 1"; 
    NSFont = "<UICTFont: 0x104249d80> font-family: \"Open Sans\"; font-weight: normal; font-style: normal; font-size: 18.00pt"; 
}Blue color{ 
    NSColor = "UIExtendedSRGBColorSpace 0 0 1 1"; 
    NSFont = "<UICTFont: 0x10b51d7c0> font-family: \"Open Sans\"; font-weight: bold; font-style: normal; font-size: 20.00pt"; 
} 

즉, 확인 같다!

보기에 적용된 동일한 코드가 적용되므로 문제는 테이블보기와 관련이 있어야합니다.

어떤 조언이 필요합니까?

업데이트

이 코드는 사용자 정의 셀보기의 방법 내부 :

컨트롤러에서
class CustomViewCell: UITableViewCell { 

    func setup() { 
     // code above 
    } 

} 

:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomIdentifier") as! CustomTableViewCell 
    cell.setup() 
    return cell 
} 
+1

스위프트 3.0은 꽤 잘 작동합니다. –

+0

여기와 동일합니다. 놀이터에서 신속하게 작동합니다. – vikingosegundo

+0

또한이 코드 조각을 게시 한 후에'self.labelInformation'과 관련된 코드가 있습니까 –

답변

-1

가 그

myMutableString.addAttribute(NSFontAttributeName, 
    //: Make a big blue P 
     myMutableString.addAttribute(
      NSFontAttributeName, 
      value: UIFont(
       name: "AmericanTypewriter-Bold", 
       size: 36.0)!, 
      range: NSRange(
       location:0, 
       length:1)) 
     myMutableString.addAttribute(
      NSForegroundColorAttributeName, 
      value: UIColor.blue(), 
      range: NSRange(
       location:0, 
       length:1)) 
처럼 뭔가를 시도

UILabel.appearance().textColor = color 

그래서 해결책은 세포 수준에서이 코드를 삽입하는 것입니다 :

Visit this link very well explained

-1

나는 내가 가지고있는 AppDelegate에에서 실수를 발견

내가이 코드의 해당하는 코드를 작성하고
override func awakeFromNib() { 
    super.awakeFromNib() 
    self.labelInformation.text = nil 
    self.labelInformation.textColor = nil 
}