Arsen Magendov
Arsen Magendov

Reputation: 37

how to define a label by a tag

i try a make a cloud of tag, and i have trouble with how to determine which label was pressed, for to the change color it. if i use like viewq.viewWithTag(1) it work but how i can understand what a label i press ? for change value. Maybe i need a make class ? because i want (if u press once, set color red and some action, if again it set blue color again and make some action)

//
//  ViewController.swift
//  weqddwqd
//
//  Created by Jacket on 4/2/18.
//  Copyright © 2018 TryFit. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var viewq: UIView!

    var arrq: [String] = ["Hello", "World", "of", "Tags"]
    var tags: [Int] = Array()

    override func viewDidLoad() {
        super.viewDidLoad()

        createTagsLabel(arrq)
    }
}

extension ViewController {

    func createTagsLabel(_: [String]) {

        var xPos:CGFloat = 15.0
        var ypos: CGFloat = 130.0
        var tag: Int = 0

        for word in arrq {

            let width = word.widthOfString(usingFont: UIFont(name:"verdana", size: 13.0)!)

            let checkWholeWidth = CGFloat(xPos) + CGFloat(width) + CGFloat(13.0) + CGFloat(25.5)
            if checkWholeWidth > UIScreen.main.bounds.size.width - 30.0 {
                xPos = 15.0
                ypos = ypos + 29.0 + 8.0
            }

            let textlable = UILabel(frame: CGRect(x: xPos, y: ypos, width: width + 18, height: 18))
            textlable.layer.borderWidth = 1.8
            textlable.layer.cornerRadius = 8
            textlable.layer.borderColor = UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0).cgColor
            textlable.textColor = UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0)

            textlable.text = word
            textlable.font = UIFont(name: "verdana", size: 13.0)
            textlable.textAlignment = .center
            textlable.isUserInteractionEnabled = true
            ypos = ypos + 25

            textlable.tag = tag
            tags.append(tag)
            tag = tag + 1

            let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapFunction(_:)))
            //textlable.addTarget(self, action: #selector(tapFunction(_:)), for: .touchUpInside)

            textlable.addGestureRecognizer(tap)
            viewq.addSubview(textlable)
        }
    }

    func tapFunction(_ recognizer: UITapGestureRecognizer) {
        print(viewq.viewWithTag(1))

        let tag = viewq.viewWithTag(1) as! UILabel
        tag.layer.borderColor = UIColor .red.cgColor
        tag.textColor = UIColor .red
        print("Added")
    }
}

extension String {

    func widthOfString(usingFont font: UIFont) -> CGFloat {
        let fontAttributes = [NSFontAttributeName: font]
        let size = self.size(attributes: fontAttributes)
        return size.width
    }

    func heightOfString(usingFont font: UIFont) -> CGFloat {
        let fontAttributes = [NSFontAttributeName: font]
        let size = self.size(attributes: fontAttributes)
        return size.height
    }
}

Upvotes: 0

Views: 47

Answers (1)

rmaddy
rmaddy

Reputation: 318794

Your tapFunction can easily get the tapped view from the gesture. No tags required:

func tapFunction(_ recognizer: UITapGestureRecognizer) {
    if let tappedLabel = recognizer.view as? UILabel {
        tappedLabel.layer.borderColor = UIColor.red.cgColor
        tappedLabel.textColor = .red
        print("Added")
    }
}

Upvotes: 1

Related Questions