I successfully added check numbers as text to a pickerView. But I wanted the text of the check numbers which had been used to be strikethrough. The code that can change a label's text to strikethrough does not work for items in a pickerView. You get something like "1023 { this is a strikethrough }" in the pickerView. Are there any fonts that have both strikethrough and normal characters? Any ideas?
Asked
Active
Viewed 662 times
2 Answers
2
So it looks like you need to use NSAttributedString objects for your picker views.
And it looks like the only solution you might have available to you is to use the UIPickerViewDelegate method pickerView:viewForRow:forComponent:reusingView:
.
If you implement that method, you'll be returning a UIView object into which you can have a UILabel which uses an attributed string.
If this were my problem, I'd probably do something like:
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
}

Community
- 1
- 1

Michael Dautermann
- 88,797
- 17
- 166
- 215
-
I did use the func you mention but used an example I found at [link](http://makeapppie.com/tag/fonts-in-uipickerview/) which worked perfectly with what I had. – Mountain Man Mar 10 '16 at 23:43
1
I ended up using the following after seeing an example at http://makeapppie.com/tag/fonts-in-uipickerview/
This function simply had to be added to existing code. I discovered that every component had to be included or the components other than the check Number component would be blank.
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
}

Mountain Man
- 252
- 2
- 10
-
Glad it worked for you, but my code uses the same basic picker view api and is quite a bit more straightforward. ;-) – Michael Dautermann Mar 11 '16 at 01:09