2014-09-17 48 views
5

Tôi có một UITableView và tôi muốn hiển thị văn bản của mỗi hàng bằng các màu khác nhau trong cùng một dòng.Thay đổi màu sắc của văn bản bằng cách sử dụng NSMutableAttributedStrings

Tôi đã thử mã này, cố gắng để dịch từ obj-C nhưng tôi không thể có nó làm việc

 let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject 

     var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description) 
     attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length)) 

     var stringToCell:String = String(format: "%@ %@", attrString, object.valueForKey("example2")!.description) 
     cell.textLabel?.text = stringToCell 

Kết quả của tất cả điều này là enter image description here

nơi số 34 tương ứng với object.valueForKey("example1")!.description , do đó, vấn đề là số lượng là không phải là màu đỏ và phần thứ hai (object.valueForKey("example2")!.description) được thay thế bằng {.

Nếu tôi để lại đoạn mã này liên quan đến NSAttributedString văn bản hàng được hiển thị chính xác.

Trả lời

11

Tôi nghĩ rằng sự cố có thể nằm trong việc gán cho cell.textLabel?.text thay vì cell.textLabel?.attributedText. Có lẽ một cái gì đó như thế này:

let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject 

var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description) 
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length)) 

var descString: NSMutableAttributedString = NSMutableAttributedString(string: String(format: " %@", object.valueForKey("example2")!.description)) 
descString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, descString.length)) 

attrString.appendAttributedString(descString); 
cell.textLabel?.attributedText = attrString 

Không chắc chắn nếu bạn muốn phần thứ hai của chuỗi có màu đỏ hoặc màu khác nên tôi làm nó thành màu đen.

1

Nếu bạn có thể, tránh sử dụng NSMutableAttributedString và chỉ cần vượt qua trong các thuộc tính với các nhà xây dựng:

private func PullToRefreshAttributedStringWithColor(color:UIColor) -> NSAttributedString { 
    return NSAttributedString(string: "Pull to Refresh", attributes: [ 
     NSForegroundColorAttributeName:color 
    ]) 
} 
1

Đây là một ví dụ về văn bản thuộc tính Swift 3

let mainText = "Here is a example of attributedString" 
let attributeText = "attributedString" 
let range = (mainText as NSString).range(of: attributeText) 
let attributedString = NSMutableAttributedString(string:mainText) 

attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range) 
attribute.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14) , range: range) 

lblTitle.attributedText = attributedString 
Các vấn đề liên quan