0

How can I have a Label in a table View recognize URLS and links When they are posted in the textField?

enter image description here

How Can i get the MessageLabel to read and Understand links and display links in the tableViewCell?

struct postStruct {
    let username : String!
    let message : String!
    let photoURL : String!
    let timeStamp: String!
    let cellUserId : String!
}



class GeneralChatroom: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

    @IBOutlet weak var messageTextField: UITextField!
    @IBOutlet weak var tableView: UITableView!

    var generalRoomDataArr = [postStruct]()



    override func viewDidLoad() {
        super.viewDidLoad()

        messageTextField.delegate = self

        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.estimatedRowHeight = 78
        self.tableView.rowHeight = UITableViewAutomaticDimension

            //Assign array values
            self.generalRoomDataArr.insert(postStruct(username: username, message: message, photoURL: firebaseUserPhotoURL, timeStamp: timeStamp, cellUserId: userIDString), at: 0)

    }//End ViewDidLoad





    //MARK:                      Buttons (BackButton || Send Button)


    @IBAction func backButtonPressed(_ sender: UIButton) {
        self.performSegue(withIdentifier: "BackToRoom", sender: nil)
    }


    //Message Send button is pressed data uploaded to firebase
    @IBAction func sendButtonPressed(_ sender: UIButton) {

        //If a character exists will be uploaded to firebase
        if ((messageTextField.text?.characters.count)! > 0) {

        let message : String = self.messageTextField.text!
        UploadGeneralChatRoom(message: message) //upload to general_room
        self.messageTextField.text = nil
        messageTextField.resignFirstResponder()//Quit keyboard

        self.tableView.reloadData() //Reload tableView
        //UploadUserData() //Update Rank in database

        }


    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return generalRoomDataArr.count // your number of cell here
    }




    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        //Transform Data From ^ to load at the bottom
        tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);

        //Set username label to display username
        let usernameLabel = cell?.viewWithTag(1) as! UILabel
        usernameLabel.text = generalRoomDataArr[indexPath.row].username


        //Set message label to display message
        let messageLabel = cell?.viewWithTag(2) as! UILabel
        messageLabel.text = generalRoomDataArr[indexPath.row].message
        messageLabel.numberOfLines = 0

        //initialize UI Profile Image
        let imageView = cell?.viewWithTag(3) as! UIImageView

        //Make Porfile Image Cirlce
        imageView.layer.cornerRadius = imageView.frame.size.width/2
        imageView.clipsToBounds = true



        //Set timeStampLabel to current time AGO
        let timeStampLabel = cell?.viewWithTag(4) as! UILabel
        timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp
        timeStampLabel.numberOfLines = 0


        return cell!
    }



    func checkCurrentPhoto() {
       // let oldImageString = index.row


    }



    func TappedOnImage(sender: UITapGestureRecognizer){
        print("Elltappy")
    }


}//END CLASS
codechicksrock
  • 291
  • 5
  • 14

1 Answers1

2

You have to use UITextView instead of UILabel as described in this post: How to intercept click on link in UITextView?

Community
  • 1
  • 1
Kyle Redfearn
  • 2,172
  • 16
  • 34