I am creating a custom keyboard. The basic principle is that start a query using FMDB and get a list of fonts (an array). After that I need to change the title of UIButton
s accordingly. But after several times, memory will reach the upper limit of the custom keyboard. Then the the keyboard restart. I think below is the source of the problem. How to modify it so that it will not draw a huge memory during each execution. Please help.
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)
}
}