UISegmentedControl 在每个分段索引中有多个字符串
UISegmentedControl with multiple strings in each segmented index
我试图在每个分段索引中显示 2 个文本字符串,而不是一个长文本。当前视图看起来像这样
我想要 "Repeat task" 和 "One time task" 下面的另一个字符串,如图中红线所示。目前我的模型是
class SegmentedControlItems:NSObject{
let title: String
let subtitle: String
init(title:String, subtitle:String) {
self.title = title
self.subtitle = subtitle
}
}
我的 table 视图 header 中的代码是
let items:[SegmentedControlItems] = {
let item1 = SegmentedControlItems(title: "Repeat task", subtitle: "2 left")
let item2 = SegmentedControlItems(title: "One time task", subtitle: "3 left")
return [item1, item2]
}()
let segmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl(items: items.map({
[=11=].title
}))
segmentedControl.tintColor = UIColor(red:0.44, green:0.75, blue:0.27, alpha:1.0)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
return segmentedControl
}()
headerView.addSubview(segmentedControl)
我在 swift4 中以编程方式创建所有视图。
有人可以帮忙吗?
用 Objective C here
找到了类似的东西
对于 iOS 11 和 Swift 4:
对于多行,试试这个。可能对你有帮助。
UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
for i in 0..<items.count {
var str = items[i].title
if items[i].subtitle != "" {
str += "\n\(items[i].subtitle)"
}
let myMutableString = NSMutableAttributedString(string: str, attributes: [:])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location:items[i].title.characters.count + 1, length:items[i].subtitle.characters.count))
myLabel[i].attributedText = myMutableString
mySegment.setTitle(myMutableString.string, forSegmentAt: i)
}
Output look like this (In UILabel its working, but not in UISegmentControl) Hope someone will do that:
我试图在每个分段索引中显示 2 个文本字符串,而不是一个长文本。当前视图看起来像这样
我想要 "Repeat task" 和 "One time task" 下面的另一个字符串,如图中红线所示。目前我的模型是
class SegmentedControlItems:NSObject{
let title: String
let subtitle: String
init(title:String, subtitle:String) {
self.title = title
self.subtitle = subtitle
}
}
我的 table 视图 header 中的代码是
let items:[SegmentedControlItems] = {
let item1 = SegmentedControlItems(title: "Repeat task", subtitle: "2 left")
let item2 = SegmentedControlItems(title: "One time task", subtitle: "3 left")
return [item1, item2]
}()
let segmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl(items: items.map({
[=11=].title
}))
segmentedControl.tintColor = UIColor(red:0.44, green:0.75, blue:0.27, alpha:1.0)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
return segmentedControl
}()
headerView.addSubview(segmentedControl)
我在 swift4 中以编程方式创建所有视图。 有人可以帮忙吗?
用 Objective C here
找到了类似的东西对于 iOS 11 和 Swift 4:
对于多行,试试这个。可能对你有帮助。
UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
for i in 0..<items.count {
var str = items[i].title
if items[i].subtitle != "" {
str += "\n\(items[i].subtitle)"
}
let myMutableString = NSMutableAttributedString(string: str, attributes: [:])
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location:items[i].title.characters.count + 1, length:items[i].subtitle.characters.count))
myLabel[i].attributedText = myMutableString
mySegment.setTitle(myMutableString.string, forSegmentAt: i)
}
Output look like this (In UILabel its working, but not in UISegmentControl) Hope someone will do that: