@@ -103,7 +103,41 @@ segmentedControl.useShadow = true
103103
104104```
105105
106+ You can add image instead of title.
106107
108+ ![ ] ( Resources/Image_with_attributes.png )
109+
110+ ``` swift
111+ // ask segmented control to initialize all elements internally
112+ segmentedControl.layoutSubviews ()
113+
114+ let imageAttachment = NSTextAttachment ()
115+ imageAttachment.image = image
116+ imageAttachment.bounds = CGRect (x : 0 , y : -5 , width : 20 , height : 20 )
117+
118+ let attributes = NSAttributedString (attachment : imageAttachment)
119+
120+ segmentedControl.changeAttributedTitle (attributes, selectedTile : attributes, atIndex : atIndex)
121+ ```
122+
123+ Or combine them.
124+
125+ ``` swift
126+ // ask segmented control to initialize all elements internally
127+ segmentedControl.layoutSubviews ()
128+
129+ let imageAttachment = NSTextAttachment ()
130+ imageAttachment.image = image
131+ imageAttachment.bounds = CGRect (x : 0 , y : -5 , width : 20 , height : 20 )
132+
133+ let attributes = segmentedControl.attributedDefaultTitles .first ? .mutableCopy () as! NSMutableAttributedString
134+ attributes.append (NSAttributedString (attachment : imageAttachment))
135+
136+ let selectedAttributes = segmentedControl.attributedSelectedTitles .first ? .mutableCopy () as! NSMutableAttributedString
137+ selectedAttributes.append (NSAttributedString (attachment : imageAttachment))
138+
139+ segmentedControl.changeAttributedTitle (attributes, selectedTile : selectedAttributes, atIndex : atIndex)
140+ ```
107141## Contribution
108142
109143Feel free to Fork, submit Pull Requests or send us your feedback and suggestions!
0 commit comments