ElaWorkshop / TagListView

Simple and highly customizable iOS tag list view, in Swift.
MIT License
2.64k stars 492 forks source link

Styling doesn't work properly when adding custom tag view #142

Open raysarebest opened 7 years ago

raysarebest commented 7 years ago

I wanted to make a custom subclass of TagView to hold onto a data structure of mine so I wouldn't have to look up the data for each tag when I check what tags are in the tag view. TagListView has an open function addTagView(_:), but the createNewTagView(_:) function that actually applies tag styling is private, which makes it impossible to make custom tag views. As it stands, this code:

let tag: TagView
func normalized(_ str: String) -> String{
    return str.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
}
if let category = allCategories?.first(where: {(type: Category) -> Bool in
    return normalized(type.shortName) == normalized(text)
}){
    tag = MHCategoryTag(category: category)
}
else{
    tag = TagView(title: text.trimmingCharacters(in: .whitespacesAndNewlines))
}
tag.paddingX = 12
tag.paddingY = 14
tag.enableRemoveButton = true
tag.textColor = superview?.backgroundColor ?? .black
tag.shadowColor = .white
tag.shadowRadius = 3
tag.shadowOpacity = 1
tag.shadowOffset = CGSize(width: 0, height: 0)
tag.removeIconLineColor = #colorLiteral(red: 0.9647058824, green: 0.4823529412, blue: 0.03137254902, alpha: 1)
tag.onLongPress = nil
tag.cornerRadius = searchBar.cornerRadius
let gradient = CAGradientLayer()
gradient.colors = [#colorLiteral(red: 0.8980392157, green: 0.9803921569, blue: 0, alpha: 1).cgColor, #colorLiteral(red: 0.9803921569, green: 0.7333333333, blue: 0, alpha: 1).cgColor]
gradient.startPoint = .zero
gradient.endPoint = CGPoint(x: 1, y: 1)
gradient.frame = tag.layer.frame
gradient.zPosition = -1
tag.layer.addSublayer(gradient)
tagView.addTagView(tag)

produces this result:

Bad tag

I had previously implemented this with the simple addTag(_:) method, which works just fine. The only change I made was to use addTagView(_:) with my own custom-made TagViews. For the record, the string input to this tag was "Thai", which probably shouldn't cause an overflow