Skip to content

Commit

Permalink
fix: fix selected title truncated issue
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Wang committed May 12, 2020
1 parent e9694a2 commit fea3699
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion SegmentedControl/SegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ open class SegmentedControl: UIControl {

open fileprivate(set) var selectedIndex = 0 {
didSet {
if selectedIndex != oldValue {
updateTitleSizes()
}
setNeedsDisplay()
}
}
Expand Down Expand Up @@ -57,7 +60,7 @@ open class SegmentedControl: UIControl {

open fileprivate(set) var titles = [NSAttributedString]() {
didSet {
titleSizes = titles.map { sizeForAttributedString($0) }
updateTitleSizes()
}
}
open fileprivate(set) var selectedTitles: [NSAttributedString]?
Expand All @@ -68,6 +71,17 @@ open class SegmentedControl: UIControl {

fileprivate var titleSizes = [CGSize]()

private func updateTitleSizes() {
titleSizes = titles.enumerated().map({ (offset, title) in
if offset == selectedIndex,
let selected = selectedTitles,
0..<selected.count ~= offset {
return sizeForAttributedString(selected[offset])
}
return sizeForAttributedString(title)
})
}

open var isLongPressEnabled = false {
didSet {
if isLongPressEnabled {
Expand Down

0 comments on commit fea3699

Please sign in to comment.