小切手番号をテキストとして pickerView に正常に追加しました。しかし、以前は取り消し線だった小切手番号のテキストが欲しかったのです。ラベルのテキストを取り消し線に変更できるコードは、pickerView の項目では機能しません。pickerView で "1023 { これは取り消し線です }" のようなものを取得します。取り消し線と通常の文字を両方持つフォントはありますか? 何か案は?
2 に答える
2
そのため、ピッカー ビューに NSAttributedString オブジェクトを使用する必要があるようです。
そして、あなたが利用できる唯一の解決策は、 UIPickerViewDelegate メソッドを使用することですpickerView:viewForRow:forComponent:reusingView:
。
そのメソッドを実装すると、属性付き文字列を使用する UILabel を持つことができる UIView オブジェクトが返されます。
これが私の問題なら、おそらく次のようにします。
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
}
于 2016-03-10T06:25:25.250 に答える
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
}
于 2016-03-10T23:47:55.877 に答える