【发布时间】:2020-06-02 14:09:24
【问题描述】:
我正在制作一个类似于谷歌文档或其他文本编辑器的简单 iOS 应用程序。我创建了一个 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:
}
你能帮帮我吗?
【问题讨论】:
-
didSelectRow的主体是空的,所以当您在选择器中选择项目时它什么也不做,将您想要的代码移到那里。 -
我要在那里放一个 switch 语句吗?
-
你到底想达到什么目的?
-
更改文本视图的字体大小
标签: ios swift xcode uipickerview