在选择器视图中为行实现的标题没有改变字体? [英] implemented title attributed for row in picker view did not change the font?
本文介绍了在选择器视图中为行实现的标题没有改变字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在我的选择器视图中更改标题的字体,但由于某种原因我不能.我可以更改标题的颜色,但字体保持不变?
I tried to change the font of the title in my picker view, but for some reason I could not. I can change the color of the title, but the font remains intact?
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let attrTitle = NSAttributedString(string: splitPickerSource[row], attributes: [NSFontAttributeName: UIFont(name: "Avenir-Heavy", size: 17.0)!, NSForegroundColorAttributeName: UIColor.darkGray])
return attrTitle
}
或者,我可以为 UIPickerView
实现 viewForRow
委托方法并从那里添加一个新标签.
Alternatively I can implement viewForRow
delegate method for the UIPickerView
and add a new label from there.
尝试打印attrTitle
Share Bill With{ NSColor = "UIExtendedSRGBColorSpace 0.117647 0.117647 0.117647 1"; NSFont = "<UICTFont: 0x7fb5f8d14080> font-family: \"Avenir-Heavy\"; font-weight: bold; font-style: normal; font-size: 14.00pt";
任何人都知道这种情况,为什么我不能修改字体attributedTitleForRow
委托方法?
Anyone has any idea about this situation and why can't I modify the font in
attributedTitleForRow
delegate method?
推荐答案
试试这个
//MARK: Setting picker select label size
/**
Called by Picker to To add customized text to fit size
- parameter pickerView: The picker view requesting the data.
- parameter row: Index of row
- parameter component: component Index
*/
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel() //get picker label that come
pickerLabel.lineBreakMode = .byWordWrapping //Wrapping if needed
pickerLabel.numberOfLines = 0; //So it comes to single line
pickerLabel.textColor = UIColor.white //setting colour
self.currentValue = NamesStringArray[row] //Array with Titles
let data = NamesStringArray[row] //Taking Index Title
let title = NSAttributedString(string: data, attributes: [NSAttributedStringKey.font : UIFont(name: "Avenir-Heavy", size: 40.0)!]) //here add attributes
pickerLabel.attributedText = title //setting attributed text as title
pickerLabel.sizeToFit() //Size to fit //If needed
pickerLabel.adjustsFontSizeToFitWidth = true //if needed
return pickerLabel //return value
}
这篇关于在选择器视图中为行实现的标题没有改变字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文