UIPickerView不允许通过委托的`attributionTitleForRow ...`更改字体名称和大小

时间:2014-12-13 03:46:26

标签: cocoa fonts size uipickerview nsattributedstring

我使用以下func更改字体颜色和字体大小,颜色有效,但字体名称和字体大小拒绝工作。

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? 

var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Arial-BoldMT", size: 45)!, NSForegroundColorAttributeName:UIColor.whiteColor()])

任何帮助?

THX。

5 个答案:

答案 0 :(得分:52)

我有另一种选择可能对你有所帮助..

完成正常选择器视图所需的所有工作:获取更多帮助UIPickerView make in Swift iOS

现在按照以下步骤操作: - 您可以像这样使用 viewForRow 的方法

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    var pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = "PickerView Cell Title"
   // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}

更新了Swift 3

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.black
    pickerLabel.text = "PickerView Cell Title"
    // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.center
    return pickerLabel
}

可能对你有帮助,我也在我的项目中使用了它。

答案 1 :(得分:9)

您可以为pickerview

声明数据源
let arrDataSource:[String] = ["row 1","row 2","row 3"]

然后使用这个字符串数组作为下面函数

中行的标题
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView

{
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = arrDataSource[row]
     pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    //pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
 }

答案 2 :(得分:1)

SWIFT 3

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

    var string = ""

    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.white
    pickerLabel.text = string
    pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22) // In this use your custom font
    pickerLabel.textAlignment = .center

    return pickerLabel

}

答案 3 :(得分:0)

要进一步详细说明UILabel的https://stackoverflow.com/users/4020910/bennythenerd答案,我将使用更多的内存优化解决方案来重用标签:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel: UILabel
    if let label = view as? UILabel {
        pickerLabel = label
    } else {
        pickerLabel = UILabel()
        pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22)
        pickerLabel.textAlignment = .center
    }
    pickerLabel.text = pickerData[row] //This is your string
    return pickerLabel
}

答案 4 :(得分:0)

Swift 4.1 / Xcode 9.4.1

很明显,您可以选择自己的字体,但是这里我使用的是System Bold 13。

<script>
function sendthedatatotheajax(sel) {
    var the_Input_Box_1_value = sel.options[sel.selectedIndex].value;

// This is where I Need Help Start
    var the_Input_Box_1_dataextra1 = [how.do.i.get.this.value?].value;
    var the_Input_Box_2_value = [again.how.do.i.get.this.value?].value;
// This is where I Need Help Stop

    $("#Output_Div_1").html( "" );
    if (the_Input_Box_1_value.length > 0 ) { 

     $.ajax({
            type: "POST",
            url: "sendbackthedata.php",
            data: {"inputbox1value": the_Input_Box_1_value, "inputbox1dataextra1": the_Input_Box_1_dataextra1, "inputbox2value": the_Input_Box_2_value},
            cache: false,
            beforeSend: function () { 
                $('#Output_Div_1').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#Output_Div_1").html( html );
            }
        });
    } 
}
</script>