Skip to content

Commit 75df904

Browse files
Fix #12: adjust Tag label size when the content is larger than the view width (#15)
1 parent df88c9f commit 75df904

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Source/WSTagView.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ public class WSTagView: UIView {
145145
return CGSize(width: labelSize.width + 2.0 * WSTagView.xPadding, height: labelSize.height + 2.0 * WSTagView.yPadding)
146146
}
147147

148+
public func sizeToFit(size: CGSize) -> CGSize {
149+
if self.frame.size.width > size.width {
150+
return CGSize(width: size.width, height: self.frame.size.height)
151+
}
152+
return self.frame.size
153+
}
154+
148155

149156
// MARK: - Attributed Text
150157

@@ -162,9 +169,7 @@ public class WSTagView: UIView {
162169
public override func layoutSubviews() {
163170
super.layoutSubviews()
164171
backgroundLayer.frame = bounds
165-
var labelFrame = CGRectInset(bounds, WSTagView.xPadding, WSTagView.yPadding)
166-
labelFrame.size.width += WSTagView.xPadding * 2.0
167-
textLabel.frame = labelFrame
172+
textLabel.frame = CGRectInset(bounds, WSTagView.xPadding, WSTagView.yPadding)
168173
}
169174

170175

Source/WSTagsField.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class WSTagsField: UIView {
8888
}
8989
}
9090

91-
public var padding: UIEdgeInsets = UIEdgeInsets(top: 10.0, left: 8.0, bottom: 10.0, right: 16.0) {
91+
public var padding: UIEdgeInsets = UIEdgeInsets(top: 10.0, left: 8.0, bottom: 10.0, right: 8.0) {
9292
didSet {
9393
repositionViews()
9494
}
@@ -181,7 +181,7 @@ public class WSTagsField: UIView {
181181
}
182182

183183
public override func intrinsicContentSize() -> CGSize {
184-
return CGSize(width: UIViewNoIntrinsicMetric, height: max(45, self.intrinsicContentHeight))
184+
return CGSize(width: self.frame.size.width - padding.left - padding.right, height: max(45, self.intrinsicContentHeight))
185185
}
186186

187187
private func repositionViews() {
@@ -195,7 +195,7 @@ public class WSTagsField: UIView {
195195
// Position Tag views
196196
var tagRect = CGRect.null
197197
for tagView in tagViews {
198-
tagRect = tagView.frame
198+
tagRect = CGRect(origin: CGPoint.zero, size: tagView.sizeToFit(self.intrinsicContentSize()))
199199

200200
let tagBoundary = isOnFirstLine ? firstLineRightBoundary : rightBoundary
201201
if curX + CGRectGetWidth(tagRect) > tagBoundary {
@@ -210,6 +210,7 @@ public class WSTagsField: UIView {
210210
// Center our tagView vertically within STANDARD_ROW_HEIGHT
211211
tagRect.origin.y = curY + ((WSTagsField.STANDARD_ROW_HEIGHT - CGRectGetHeight(tagRect))/2.0)
212212
tagView.frame = tagRect
213+
tagView.setNeedsLayout()
213214

214215
curX = CGRectGetMaxX(tagRect) + WSTagsField.HSPACE + self.spaceBetweenTags
215216
}
@@ -279,7 +280,6 @@ public class WSTagsField: UIView {
279280
return self.textField.editing
280281
}
281282

282-
283283
public func beginEditing() {
284284
self.textField.becomeFirstResponder()
285285
self.unselectAllTagViewsAnimated(false)

0 commit comments

Comments
 (0)