1

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
  }

 }
Noorul
  • 3,386
  • 3
  • 32
  • 54
  • 2
    Is `override init(style: UITableViewCellStyle, reuseIdentifier: String!) ` called? How did you create your `UITableViewCell` (to your `UITableView`)? Storyboard? Code? – Larme Jul 10 '17 at 08:55
  • I posted my code. Kindly check it. – Noorul Jul 10 '17 at 08:59
  • and also i want to place the image to the right of the label.percentage image is added in images.xcassets. THat thing also not working – Noorul Jul 10 '17 at 09:01
  • The question is how are you creating the cells. Yes you posted the init function itself. But how are you creating the cells? Can you post your `cellForRow` method please? – Fangming Jul 10 '17 at 09:49

1 Answers1

0

When you create your custom cell in this way, only required init?(coder aDecoder: NSCoder) { is called, not your override init(style: UITableViewCellStyle, reuseIdentifier: String!).

You can either move your label set up code into the required init method, or do so in your cellForRow method.

Fangming
  • 24,551
  • 6
  • 100
  • 90
  • thats fine. thanks for replying. but the code is not make the round. it makes oval shape textview. And one more thing. is there any best and easiest tutorial for constrain layout and for size classes. I need responsive mobile application. kindly share the link. – Noorul Jul 11 '17 at 05:03
  • @Noorul Check out this link to programatically add constraints https://stackoverflow.com/a/36263784/5838902 – Fangming Jul 11 '17 at 05:31