我正在创建一个自定义键盘。基本原则是使用FMDB启动查询并获取字体列表(数组)。之后我需要相应地更改UIButton
s的标题。但经过好几次,内存将达到自定义键盘的上限。然后键盘重启。我认为下面是问题的根源。如何修改它以便在每次执行期间不会占用大量内存。请帮忙。
func create_font_button(){
let u = UIScreen.main.bounds.width/8
font_view.frame = CGRect(x: 0, y:0, width:mainview.bounds.size.width, height: UIScreen.main.bounds.height / 45 * 4)
font_view.backgroundColor = UIColor.white
self.view.addSubview(font_view)
for index in 0...7 {
font_btn = UIButton(type: .roundedRect)
font_btn.tag = index
font_btn.backgroundColor = UIColor.yellow
font_btn.titleLabel!.font = UIFont(name: "Helvetica-Bold", size: 30)
font_btn.frame = CGRect(x:u * CGFloat(index), y: 0, width: u-1, height: UIScreen.main.bounds.height / 45 * 4)
font_btn.titleLabel?.minimumScaleFactor = 0.1
font_btn.titleLabel?.numberOfLines = 1
font_btn.titleLabel?.adjustsFontSizeToFitWidth = true
if index == 0{
if font.count == 0{
font_view.removeFromSuperview()
}
else{
font_btn.backgroundColor = UIColor(red: (255/255.0), green: (204/255.0), blue: (204/255.0), alpha: 1.0)
font_btn.setTitle(font[0], for: .normal)
font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
}
}
else if index == 1{
if font.count < 2{
font_btn.backgroundColor = UIColor.white
font_btn.setTitle("", for: .normal)
}
else{
font_btn.backgroundColor = UIColor(red: (196/255.0), green: (240/255.0), blue: (244/255.0), alpha: 1.0)
font_btn.setTitle(font[1], for: .normal)
font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
}
}
else if index == 2{
if font.count < 3 {
font_btn.backgroundColor = UIColor.white
font_btn.setTitle("", for: .normal)
}
else{
font_btn.backgroundColor = UIColor(red: (255/255.0), green: (254/255.0), blue: (204/255.0), alpha: 1.0)
font_btn.setTitle(font[2], for: .normal)
font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
}
}
else{
if font.count < index + 1{
font_btn.backgroundColor = UIColor.white
font_btn.setTitle("", for: .normal)
}
else{
font_btn.backgroundColor = UIColor(red: 248.0/255, green: 242.0/255, blue: 227.0/255, alpha: 1)
font_btn.setTitle(font[index], for: .normal)
font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
}
}
font_view.addSubview(font_btn)
}
}