[英]Rounded Label is not working in swift xcode
我是快速进行IOS编程的新手。 我需要使标签四舍五入。 我已经在SO中搜索了代码,并从头抓到了我的应用程序,该应用程序被接受并接受了10多个投票,但是,在我的情况下,该代码无法正常工作。
码
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
}
我在以下构造器的UITableViewCell class ..上实现了它。
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
changeToRoundLable(countLabel: txtDays)
}
我不知道我在哪里弄错了。 请帮助我。
XCode版本:8.3.3(8E3004b)Swift版本:Apple Swift版本3.1
更新
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()
}
}
通过以下方式创建单元:
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
}
}
当您以这种方式创建自定义单元格时,仅调用required init?(coder aDecoder: NSCoder) {
,而不是override init(style: UITableViewCellStyle, reuseIdentifier: String!)
。
您可以将标签设置代码移动到所需的init方法中,也可以在cellForRow
方法中进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.