简体   繁体   English

Swift iOS - 标签集合视图

[英]Swift iOS - Tag collection view

I'm writing my first iOS app and I wanna just answer what is the best-known solution to make this?我正在编写我的第一个 iOS 应用程序,我只想回答最广为人知的解决方案是什么? It's simple tag collection.这是简单的标签集合。 I have already looked over the Internet but I have found nothing.我已经查看了互联网,但我什么也没找到。 I think the best way is to make my own structure of buttons maybe?我认为最好的方法是制作我自己的按钮结构?

Here is what I want to achieve:这是我想要实现的目标:

Swift 标签集合视图

在此处输入图片说明 I have implemented a tagview using just a collectionview.我只使用 collectionview 实现了一个 tagview。

Here is the github link.是github链接。

I resolve this problem, using collection view.我使用集合视图解决了这个问题。

class FilterController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    @IBOutlet var collectionView: UICollectionView?


    override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view, typically from a nib.
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 150, left: 10, bottom: 150, right: 10)
    // layout.itemSize = CGSize(width: 90, height: 45)
    layout.itemSize = CGSizeFromString("Aloha")

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView!.dataSource = self
    collectionView!.delegate = self
    collectionView!.registerClass(TagCell.self, forCellWithReuseIdentifier: "TagCell")
    collectionView!.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(collectionView!)
}

sometimes you need do it yourself:有时你需要自己做:

import UIKit
import PlaygroundSupport

class TagsView: UIView {

    // MARK: - Properties

    var offset: CGFloat = 5

    // MARK: - Public functions

    func create(cloud tags: [UIButton]) {
        var x = offset
        var y = offset
        for (index, tag) in tags.enumerated() {
            tag.frame = CGRect(x: x, y: y, width: tag.frame.width, height: tag.frame.height)
            x += tag.frame.width + offset

            let nextTag = index <= tags.count - 2 ? tags[index + 1] : tags[index]
            let nextTagWidth = nextTag.frame.width + offset

            if x + nextTagWidth > frame.width {
                x = offset
                y += tag.frame.height + offset
            }

            addSubview(tag)
        }
    }
}

private func button(with title: String) -> UIButton {
    let font = UIFont.preferredFont(forTextStyle: .headline)
    let attributes: [NSAttributedString.Key: Any] = [.font: font]
    let size = title.size(withAttributes: attributes)

    let button = UIButton(type: .custom)
    button.setTitle(title, for: .normal)
    button.titleLabel?.font = font
    button.setTitleColor(.darkGray, for: .normal)
    button.layer.borderWidth = 1.0
    button.layer.cornerRadius = size.height / 2
    button.layer.borderColor = UIColor.darkGray.cgColor
    button.frame = CGRect(x: 0.0, y: 0.0, width: size.width + 10.0, height: size.height + 10.0)
    button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 5.0, bottom: 0.0, right: 5.0)
    return button
}

let titles = ["Freedom", "God", "Happiness", "Imagination", "Intelligence", "Other"]
let tags = titles.map { button(with: $0) }

let frame = CGRect(x: 0, y: 0, width: 260, height: 200)
let tagsView = TagsView(frame: frame)
tagsView.backgroundColor = .white
tagsView.create(cloud: tags)

PlaygroundPage.current.liveView = tagsView
PlaygroundPage.current.needsIndefiniteExecution = true

只需将按钮动态添加到 superView,并根据您的要求更改背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM