简体   繁体   English

如何在Swift 2.2中的Pickerview中更改文本/字体大小?

[英]How do i change the text / font size in pickerview in swift 2.2?

我在旧版本的swift中看到了这里的解释。必须有其他的覆盖方法和编码,这将允许我在swift 2.2中更改文本大小。

To change the font name and size you can use viewForRow and an attributed string: 要更改字体名称和大小,可以使用viewForRow和属性字符串:

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

var label = view as! UILabel!
if label == nil {
    label = UILabel()
}

var data = pickerData[row]
let title = NSAttributedString(string: data!, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(36.0, weight: UIFontWeightRegular)])
label.attributedText = title
label.textAlignment = .Center
return label

}

And if you make your font size larger you'll want to increase the height of each row with rowHeightForComponent: 而且,如果您增大字体大小,则需要使用rowHeightForComponent增加每行的高度:

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 36.0
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift 中更改 UILabel 的字体大小? - How do I change the font size of a UILabel in Swift? 如何在 WKWebView swift 5 中更改字体大小? - How do i change font size in WKWebView swift 5? 如何在 Swift 的 UIPickerView 中更改字体大小? - How do I change the font size in a UIPickerView in Swift? 如何更改文本视图中的字体大小,而不更改字体样式? (轻量还是常规)-迅捷 - How can I change the font size in a text view, but not the font style? (light vs. regular) - Swift 如何在不改变字体大小的情况下在swift中更改UILabel的字体? - How do I change the font of a UILabel in swift without changing the font size? 如何在不更改字体大小的情况下更改swift中UISegmentedControl的字体? - How do I change the font of a UISegmentedControl in swift without changing the font size? 如何在Swift中更改UIBarButtonItem的字体? - How do I change the font of a UIBarButtonItem in Swift? 如何在 Swift 中更改导航栏字体? - How do I change navigationBar font in Swift? 如何在 Swiftui 中使用相同的字体更改字体大小 - How do i change the font size with still the same font in Swiftui 我如何设置一个带有 4 个组件的 pickerView,Arry? 迅速 - How do I setUp a pickerView with 4 components, Arry? Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM