2016-03-10 18 views
0

我成功將檢查號碼作爲文本添加到pickerView中。但我想要的是過去使用的支票號碼的文本是刪除線。可以將標籤文本更改爲刪除線的代碼不適用於pickerView中的項目。你會在pickerView中看到「1023 {這是一個刪除線}」。有沒有帶刪除線和普通字符的字體?有任何想法嗎?在Swift中使用pickerView中的文本刪除線

回答

1

我最終使用在http://makeapppie.com/tag/fonts-in-uipickerview/

看到一個例子後,下面這個函數簡單不得不被添加到現有碼。我發現必須包含每個組件,否則檢查號組件以外的組件將爲空白。

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


    let myHueMax = 0.50 
    let myPickerFontSize: CGFloat = 20.0 
    print("component in lable == \(component)") 

    var pickerLabel = view as! UILabel! 
    var gHue = Double() 


    if view == nil { //if no label there yet 
     pickerLabel = UILabel() 
     //color the label's background 

     var hue:CGFloat 

     if component == payeeComponent { 

      hue = CGFloat(row)/CGFloat(payeesAlphbeticalArray.count) 

     }else if component == categoryComponent { 

      hue = CGFloat(row)/CGFloat(categoriesAlphbeticalArray.count) 

     }else{ 

      hue = CGFloat(row)/CGFloat(checkNumbersAlphbeticalArray.count) 

     } 


     pickerLabel!.backgroundColor = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0) 
     print("hue in label color == \(hue)") 

     gHue = Double(hue) 
    } 

if component == payeeComponent { 



    let titleData = payeesAlphbeticalArray[row] 

    var myTitle: NSAttributedString 

    if (gHue > myHueMax){ 
     myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.whiteColor()]) 
    }else{ 

     myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.blackColor()]) 
    } 

    pickerLabel!.attributedText = myTitle 
    pickerLabel!.textAlignment = .Center 


    return pickerLabel! 



}else if component == categoryComponent{ 

    let titleData = categoriesAlphbeticalArray[row] 

    var myTitle: NSAttributedString 

    if (gHue > myHueMax){ 
     myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.whiteColor()]) 
    }else{ 

     myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.blackColor()]) 
    } 


    pickerLabel!.attributedText = myTitle 
    pickerLabel!.textAlignment = .Center 


    return pickerLabel! 



}else if component == checkNumberComponent { 


    if checkNumbersAlphbeticalArray.isEmpty{ 

     return pickerLabel! 

    } 

    let titleData = checkNumbersAlphbeticalArray[row] 

    var myTitle: NSAttributedString 



    if isCheckNumberUsed(titleData){ 

     if (gHue > myHueMax){ 
      myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.whiteColor(), NSStrikethroughStyleAttributeName: 1]) 
     }else{ 

      myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.blackColor(), NSStrikethroughStyleAttributeName: 1]) 
     } 

    }else{ 

     if (gHue > myHueMax){ 
      myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.whiteColor()]) 
     }else{ 

      myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: myPickerFontSize)!,NSForegroundColorAttributeName:UIColor.blackColor()]) 
     } 
    } 
    pickerLabel!.attributedText = myTitle 
    pickerLabel!.textAlignment = .Center 


    return pickerLabel! 

    } 

    return pickerLabel 

} 
+0

很高興爲你工作,但我的代碼使用相同的基本選擇器視圖API,並且更直截了當。 ;-) –

2

所以看起來你需要爲你的選擇器視圖使用NSAttributedString對象。

it looks like the only solution you might have available to you是使用UIPickerViewDelegate方法pickerView:viewForRow:forComponent:reusingView:

如果您實現該方法,您將返回一個UIView對象,您可以在其中使用一個使用屬性字符串的UILabel。

如果這是我的問題,我可能會做這樣的事情:

func pickerView(_ pickerView: UIPickerView, 
       viewForRow row: Int, 
      forComponent component: Int, 
      reusingView view: UIView?) -> UIView 
{ 
    // create a mutable attributed string 
    let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here") 
    // add strikethrough attribute to the whole string 
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 

    // set up a label 
    let pickerLabel = UILabel(frame: CGRectMake(0, 0, 200, 21)) 
    pickerLabel.center = CGPointMake(160, 284) 
    pickerLabel.textAlignment = NSTextAlignment.Center 

    // and set the contents to the atributedString 
    pickerLabel.attributedText = attributeString 

    return pickerLabel 
} 
+0

我沒有使用你提到的FUNC,但使用我發現一個例子[鏈接](http://makeapppie.com/tag/fonts-in-uipickerview/),這與我有完美。 –

相關問題