Skip to content

Commit

Permalink
Merge pull request #13 from teambition/fix/selected-title-truncated
Browse files Browse the repository at this point in the history
fix: fix selected title truncated issue
  • Loading branch information
suricforever authored May 12, 2020
2 parents e9694a2 + fea3699 commit d4b94b9
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 d4b94b9

Please sign in to comment.