我成功將檢查號碼作爲文本添加到pickerView中。但我想要的是過去使用的支票號碼的文本是刪除線。可以將標籤文本更改爲刪除線的代碼不適用於pickerView中的項目。你會在pickerView中看到「1023 {這是一個刪除線}」。有沒有帶刪除線和普通字符的字體?有任何想法嗎?在Swift中使用pickerView中的文本刪除線
0
A
回答
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
}
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/),這與我有完美。 –
相關問題
- 1. 如何在Swift中使用NSUserDefault和PickerView?
- 2. 刪除線在foreach中的文本?
- 3. Swift - 在UIViewController的文本框中添加一個pickerView
- 4. 使用Swift從文本文件中刪除單行
- 5. 如何更改swift 2.2中pickerview中的文本/字體大小?
- 6. Android:LIstView中的刪除線文本
- 7. 使用CSS刪除元素中h文本的下劃線
- 8. Animate PickerView Height Swift
- 9. 用Swift從文本文件中刪除單詞
- 10. 在swift中刪除BlurView
- 11. 使用swift從URL中刪除參數
- 12. 在iOS中刪除URL中的「\」Swift
- 13. Android ListView刪除線文本
- 14. swift中的pickerView不顯示數據
- 15. 在文本框點擊pickerview改變文本框輸入視圖從pickerview到鍵盤在SWIFT 3.0
- 16. 使用jquery從div中刪除文本
- 17. 使用ruby從文本中刪除url
- 18. 在outlet集合文本框中使用pickerView
- 19. 如何使用swift 3中的按鈕清除文本字段中的文本?
- 20. 在文件中刪除線使用的sed
- 21. 如何刪除iPhone中pickerview中的重複數據?
- 22. 如何在Swift 3中用戶觸摸時從UITextField中刪除文本?
- 23. 如何從iOS Swift 3中的餅圖部分刪除文本?
- 24. 在刪除文本文件中的行後刪除空白VB6
- 25. 在Swift中使用UITextView包裝文本
- 26. 如何在oracle中刪除短劃線之後的文本?
- 27. 如何在Android Studio中刪除純文本的下劃線
- 28. 使用SED刪除文本
- 29. 使用javascript刪除文本
- 30. 用swift刪除cloudkit中的數據
很高興爲你工作,但我的代碼使用相同的基本選擇器視圖API,並且更直截了當。 ;-) –