Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let developer specify extra attributes to unselected and selected segment title strings #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion TTSegmentedControl/TTSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ open class TTSegmentedControl: UIView {
@IBInspectable open var selectedTextFont: UIFont = UIFont.helveticaNeueLight(12)
@IBInspectable open var defaultTextColor: UIColor = UIColor.black
@IBInspectable open var selectedTextColor: UIColor = UIColor.white
open var defaultTextAttributes: [NSAttributedString.Key: Any]?
open var selectedTextAttributes: [NSAttributedString.Key: Any]?
@IBInspectable open var useGradient: Bool = true

@IBInspectable open var containerBackgroundColor: UIColor = TTSegmentedControl.UIColorFromRGB(0xF4F4F4)
Expand Down Expand Up @@ -311,8 +313,15 @@ extension TTSegmentedControl {
let textColor = isSelected ? selectedTextColor : defaultTextColor
let textFont = isSelected ? selectedTextFont : defaultTextFont

let attributes = [NSAttributedString.Key.foregroundColor : textColor,
var attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor : textColor,
NSAttributedString.Key.font : textFont]

if isSelected && self.selectedTextAttributes != nil {
attributes.merge(self.selectedTextAttributes!, uniquingKeysWith: { _, userDefined in userDefined })
}else if self.defaultTextAttributes != nil {
attributes.merge(self.defaultTextAttributes!, uniquingKeysWith: { _, userDefined in userDefined })
}

let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
return attributedString
}
Expand Down