gpt4 book ai didi

swift - 在 Swift 中将 pickerView 中的文本设为删除线

转载 作者:行者123 更新时间:2023-11-28 11:09:41 25 4
gpt4 key购买 nike

我成功地将支票号码作为文本添加到 pickerView。但我希望用于删除线的支票号码的文本。可以将标签文本更改为删除线的代码不适用于 pickerView 中的项目。您会在 pickerView 中看到类似“1023 { 这是删除线 }” 的内容。是否有同时具有删除线和普通字符的字体?有任何想法吗?

最佳答案

所以看起来您需要为选择器 View 使用 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
}

关于swift - 在 Swift 中将 pickerView 中的文本设为删除线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com