I am new to swift IOS programming. I need to make the label rounded. I have searched the code in SO and scratch to my app which is accepted answer and upvoted more than 10. but , in my case that code is not working.
CODE
func changeToRoundLable(countLabel : UILabel){
let size:CGFloat = 55.0
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height : size)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor
countLabel.translatesAutoresizingMaskIntoConstraints = false
}
I implemented it on UITableViewCell class.in following contructor.
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
changeToRoundLable(countLabel: txtDays)
}
i don't know where i made the mistake. Kindly help me.
XCode Version: 8.3.3 (8E3004b) Swift Version: Apple Swift version 3.1
UPDATE
import UIKit
class ProductListItemCell: UITableViewCell {
@IBOutlet weak var txtOfferPercentage: UILabel!
@IBOutlet weak var txtDays: UILabel!
@IBAction func btnViewDeal(_ sender: UIButton) {
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
changeToRoundLable(countLabel: txtDays)
offerPercentageImage()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func offerPercentageImage(){
let point = CGPoint(x:10,y:10);
let size = CGSize(width:txtOfferPercentage.frame.size.width - 20,height:20)
let labelLeft = SMIconLabel(frame: CGRect(origin:point,size: size))
labelLeft.text = "Icon on the left, text on the left"
// Here is the magic
labelLeft.icon = UIImage(named: "percentage") // Set icon image
labelLeft.iconPadding = 5 // Set padding between icon and label
labelLeft.numberOfLines = 0 // Icon position
labelLeft.iconPosition.horizontal = SMIconHorizontalPosition.right
txtOfferPercentage.addSubview(labelLeft)
}
func changeToRoundLable(countLabel : UILabel){
let size:CGFloat = 55.0
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height : size)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor
countLabel.translatesAutoresizingMaskIntoConstraints = false
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
Creating Cell in following Way:
class ProductListPageController:
UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tblProductList: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// tabname.text = tabText!
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tblProductList.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ProductListItemCell
cell.imgProduct.image = UIImage(named: "burger")
return cell
}
}