Swift 在快速删除线的pickerView中创建文本
我成功地将支票号码作为文本添加到pickerView中。但是我想要支票号码的文本,以前的支票号码是删除线的。可以将标签文本更改为删除线的代码不适用于pickerView中的项。在pickerView中会出现类似“1023{这是删除线}”的内容。有没有同时包含删除线字符和普通字符的字体?有什么想法吗 看来您需要在选择器视图中使用NSAttributedString对象 并使用UIPickerViewDelegate方法 如果实现该方法,将返回一个UIView对象,其中可以包含使用属性字符串的UILabel 如果这是我的问题,我可能会这样做:Swift 在快速删除线的pickerView中创建文本,swift,uipickerview,strikethrough,Swift,Uipickerview,Strikethrough,我成功地将支票号码作为文本添加到pickerView中。但是我想要支票号码的文本,以前的支票号码是删除线的。可以将标签文本更改为删除线的代码不适用于pickerView中的项。在pickerView中会出现类似“1023{这是删除线}”的内容。有没有同时包含删除线字符和普通字符的字体?有什么想法吗 看来您需要在选择器视图中使用NSAttributedString对象 并使用UIPickerViewDelegate方法 如果实现该方法,将返回一个UIView对象,其中可以包含使用属性字符串的UIL
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
}
因此,您似乎需要在选择器视图中使用NSAttributedString对象 并使用UIPickerViewDelegate方法 如果实现该方法,将返回一个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
}
在上看到一个示例后,我最终使用了以下内容 只需将此函数添加到现有代码中即可。我发现必须包含每个组件,否则除了支票号码组件之外的组件将为空
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
}
在上看到一个示例后,我最终使用了以下内容 只需将此函数添加到现有代码中即可。我发现必须包含每个组件,否则除了支票号码组件之外的组件将为空
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
}
我确实使用了你提到的func,但使用了一个我发现它与我所拥有的完美结合的示例。我确实使用了你提到的func,但使用了一个我发现它与我所拥有的完美结合的示例。很高兴它对你起到了作用,但我的代码使用了相同的基本picker view api,并且更加简单明了。;-)很高兴它能为您工作,但我的代码使用了相同的基本picker-view api,并且更加简单明了。;-)