Skip to content

Commit

Permalink
Update cell label frames when badge present
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdvs committed Sep 8, 2016
1 parent 28b819d commit cda4294
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
8 changes: 7 additions & 1 deletion TDBadgedCell/TDBadgedCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class TDBadgedCell: UITableViewCell {

badgeView.frame.origin.x = floor(self.contentView.frame.width - badgeView.frame.width - badgeOffset.x)
badgeView.frame.origin.y = floor((self.frame.height / 2) - (badgeView.frame.height / 2))

// Now lets update the width of the cells text labels to take the badge into account
self.textLabel?.frame.size.width -= badgeView.frame.width + (badgeOffset.x * 2)
if((self.detailTextLabel) != nil) {
self.detailTextLabel?.frame.size.width -= badgeView.frame.width + (badgeOffset.x * 2)
}
}

// When the badge
Expand Down Expand Up @@ -92,7 +98,7 @@ class TDBadgedCell: UITableViewCell {
NSString(string: badgeString).draw(in:CGRect(x:8, y:5, width:textSize.width, height:textSize.height), withAttributes: [
NSFontAttributeName:UIFont.boldSystemFont(ofSize:CGFloat(badgeFontSize)),
NSForegroundColorAttributeName: UIColor.clear
])
])

let badgeImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
Expand Down
11 changes: 1 addition & 10 deletions TDBadgedCell/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,14 @@ class ViewController: UITableViewController {
["title" : "This is an example badge", "badge": "1"],
["title" : "This is a second example badge", "badge": "123"],
["title" : "A text badge", "badge": "Warning!"],
["title" : "Another text badge", "badge": "Danger!"],
["title" : "Another text badge with a really long title!", "badge": "Danger!"],
]

override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return demoItems.count
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> TDBadgedCell {
var cell : TDBadgedCell? = tableView.dequeueReusableCell(withIdentifier:"BadgedCell") as! TDBadgedCell?;
if((cell == nil)) {
Expand Down

0 comments on commit cda4294

Please sign in to comment.