我正在制作一个简单的iOS应用程序,类似于google docs或其他文本编辑器。我创建了一个UIPickerview来改变字体大小,但后来我意识到,当我选择一些东西时,什么都没有发生。那么有没有什么方法可以做到这一点,下面是我的代码。
import UIKit
class FirstViewController: UIViewController, UIPopoverPresentationControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var fontBTN: UIButton!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arrayFruits.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arrayFruits[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
var arrayFruits = [String]()
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var aligners: UISegmentedControl!
@IBOutlet weak var foramtters: UISegmentedControl!
@IBOutlet weak var textView: UITextView!
var pickerData = ["10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "36", "48", "72"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.picker.delegate = self
self.picker.dataSource = self
arrayFruits = ["10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "36", "48", "72"]
}
@IBAction func aligning(_ sender: Any) {
switch aligners.selectedSegmentIndex {
case 0:
textView.textAlignment = .left
case 1:
textView.textAlignment = .center
case 2:
textView.textAlignment = .right
case 3:
textView.textAlignment = .justified
default: break
}
}
@IBAction func formating(_ sender: Any) {
switch foramtters.selectedSegmentIndex {
case 0:
textView.font = UIFont.boldSystemFont(ofSize: 14)
case 1:
textView.attributedText = NSAttributedString(string: textView.text, attributes:
[.underlineStyle: NSUnderlineStyle.single.rawValue])
textView.font = UIFont(name: "Arial", size: 14)
case 2:
textView.font = UIFont.italicSystemFont(ofSize: 14)
default: break
}
}
@IBAction func fontPicker(_ sender: Any) {
if picker.isHidden == true {
picker.isHidden = false
fontBTN.setTitle("Done", for: .normal)
} else if picker.isHidden == false {
fontBTN.setTitle("Aa", for: .normal)
picker.isHidden = true
}
}
}
我试过做一个 Switch statement
但我收到一个错误信息,说 "Expression pattern of type 'Int' cannot match values of type 'UIPickerView?'"
我试着上网查了一下,但什么也没查到,所以我问了这个问题。
switch picker {
case 0:
//funtion here
default:
}
能否请你帮帮我?
考虑以下代码。
class ViewController: UIViewController {
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var textView: UITextView!
let data: [CGFloat] = [20, 25, 30]
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
}
}
extension ViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textView.font = UIFont.systemFont(ofSize: data[row])
}
}
extension ViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(data[row])"
}
}
因此,你可以运行基于UIPickerView选择的代码 通过实现的UIPickerView选择 didSelectRow
方法如上图所示。编辑。 我修改了代码来更新textView的字体大小。
要根据选择器的选择来改变textView的字体大小,你需要做这样的事情。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if let doubleValue = Double(arrayFruits[row]) {
let fontSize = CGFloat(doubleValue)
textView.font = UIFont.systemFont(ofSize: fontSize)
}
}