From fea36996401b5453c001ecd2f711ad7f3891fd59 Mon Sep 17 00:00:00 2001 From: WangWei Date: Tue, 12 May 2020 19:26:54 +0800 Subject: [PATCH] fix: fix selected title truncated issue --- SegmentedControl/SegmentedControl.swift | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/SegmentedControl/SegmentedControl.swift b/SegmentedControl/SegmentedControl.swift index d336d9e..6ad026e 100644 --- a/SegmentedControl/SegmentedControl.swift +++ b/SegmentedControl/SegmentedControl.swift @@ -15,6 +15,9 @@ open class SegmentedControl: UIControl { open fileprivate(set) var selectedIndex = 0 { didSet { + if selectedIndex != oldValue { + updateTitleSizes() + } setNeedsDisplay() } } @@ -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]? @@ -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..