I have to create tap on link from label
,so I found this link https://github.com/d6u/TapLabel.When I create demo ,it's working on View
and tableview
. But when I use this TapLabel example in my project ,it's not working. Actually I need tap link in my chat view , my view is look like a messenger. Here is my code. The delegate
method is not working.
This is my cell with taplabel delegate method
class BHBMessageCell: UITableViewCell {
let headerImgView:UIImageView = UIImageView()
// let contentImgView:UIImageView = UIImageView()
//let contentLbl:UILabel = UILabel()
let contentLbl = TapLabel()
let bubbleImgView:UIImageView = UIImageView()
var delegate: AppDelegate!
let aDataBase : databaseinit = databaseinit()
var message:BHBMessage?{//Construction of cell layout model based on message
didSet{
delegate = UIApplication.sharedApplication().delegate as! AppDelegate
self.headerImgView.removeFromSuperview()
self.contentLbl.removeFromSuperview()
self.bubbleImgView.removeFromSuperview()
self.contentView.addSubview(self.headerImgView)
self.contentView.addSubview(self.bubbleImgView)
self.contentLbl.font = UIFont (name: "Arial", size: 14)
self.headerImgView.layer.cornerRadius = 9.0
self.headerImgView.layer.masksToBounds = true
self.contentLbl.delegate = self;
//he data message to the model head , content, bubble view
if(message?.role == Role.Sender)
{
//**************** Actually this is sender from Database
// Yaha par ulta hai
// this is change right to left
if(self.delegate.MyImage != nil)
{
self.headerImgView.image = self.delegate.MyImage
}else
{
self.headerImgView.image = UIImage(named: "User-yellow-icon.png")
}
}else
{
//**************** Actually this is Receiver from Database
if(self.delegate.FrontImage != nil)
{
self.headerImgView.image = self.delegate.FrontImage
}else
{
self.headerImgView.image = UIImage(named: "User-yellow-icon.png")
}
}
// self.headerImgView.image = message?.role == Role.Sender ? UIImage(named: "icon01") : self.delegate.FrontImage
self.bubbleImgView.image = message?.role != Role.Receive ? UIImage(named: "chatto_bg_normal") : UIImage(named: "chatfrom_bg_normal")
let str:String! = message?.content
let text = NSMutableAttributedString(string: str, attributes: [
NSFontAttributeName: UIFont.systemFontOfSize(12),
NSParagraphStyleAttributeName: {
let p = NSMutableParagraphStyle()
p.lineSpacing = 5
p.alignment = NSTextAlignment.Center
return p
}()
])
var splitStringArray = str.componentsSeparatedByString(" ")
for var i = 0; i < splitStringArray.count ; i++
{
let word:String = splitStringArray[i];
if word.hasPrefix("www") || word.hasPrefix("http")
{
let range: Range<String.Index> = (str.rangeOfString(word)!)
let start:Int = Int("\(range.startIndex)")!
let end:Int = word.characters.count
// print(start)
text.addAttribute(TapLabel.LinkContentName, value: word, range: NSMakeRange(start, end))
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSMakeRange(start, end))
text.addAttribute(TapLabel.SelectedForegroudColorName, value: UIColor.redColor(), range: NSMakeRange(start, end))
}
}
self.contentLbl.attributedText = text
self.contentLbl.lineBreakMode = .ByWordWrapping
self.contentLbl.sizeToFit()
self.bubbleImgView.addSubview(self.contentLbl)// Add label
self.contentLbl.delegate = self
self.headerImgView.translatesAutoresizingMaskIntoConstraints = false
self.contentLbl.translatesAutoresizingMaskIntoConstraints = false
self.bubbleImgView.translatesAutoresizingMaskIntoConstraints = false
self.contentLbl.textAlignment = message?.role != Role.Receive ? NSTextAlignment.Right : NSTextAlignment.Left
self.contentLbl.numberOfLines = 0
var viewsDictionary : Dictionary<String,AnyObject>
viewsDictionary = ["header": self.headerImgView, "content": self.contentLbl, "bubble": self.bubbleImgView]
var header_constraint_H_Format = ""
var header_constraint_V_Format = ""
var bubble_constraint_H_Format = ""
var bubble_constraint_V_Format = ""
var content_constraint_H_Format = ""
var content_constraint_V_Format = ""
if message?.role == Role.Sender {
header_constraint_H_Format = "[header(50)]-5-|"
header_constraint_V_Format = "V:|-5-[header(50)]"
bubble_constraint_H_Format = "|-(>=5)-[bubble]-10-[header]"
bubble_constraint_V_Format = "V:|-5-[bubble(>=50)]-5-|"
content_constraint_H_Format = "|-(>=5)-[content]-25-|"
content_constraint_V_Format = "V:|[content]-5-|"
} else {
header_constraint_H_Format = "|-5-[header(50)]"
header_constraint_V_Format = "V:|-5-[header(50)]"
bubble_constraint_H_Format = "[header]-10-[bubble]-(>=5)-|"
bubble_constraint_V_Format = "V:|-5-[bubble(>=50)]-5-|"
content_constraint_H_Format = "|-25-[content]-(>=5)-|"
content_constraint_V_Format = "V:|[content]-5-|"
}
let header_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let header_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_V_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let bubble_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(bubble_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let bubble_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(bubble_constraint_V_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let content_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(content_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let content_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(content_constraint_V_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
self.contentView.addConstraints(header_constraint_H as! [NSLayoutConstraint])
self.contentView.addConstraints(header_constraint_V as! [NSLayoutConstraint])
self.contentView.addConstraints(bubble_constraint_H as! [NSLayoutConstraint])
self.contentView.addConstraints(bubble_constraint_V as! [NSLayoutConstraint])
self.bubbleImgView.addConstraints(content_constraint_H as! [NSLayoutConstraint])
self.bubbleImgView.addConstraints(content_constraint_V as! [NSLayoutConstraint])
}
}
}
extension BHBMessageCell : TapLabelDelegate
{
func tapLabel(tapLabel: TapLabel, didSelectLink link: String) {
print(link)
}
}
**Here is my view tableview cell **
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let getMessage:String! = self.messages[indexPath.row].content as String
let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "cellLongPressed:")
longPress.delegate = self
longPress.minimumPressDuration = 1
longPress.numberOfTouchesRequired = 1
// print(self.messages[indexPath.row].msg_images)
let cell = tableView .dequeueReusableCellWithIdentifier("BHBMessageCell") as! BHBMessageCell
cell.addGestureRecognizer(longPress)
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.message = self.messages[indexPath.row]
cell.backgroundColor = UIColor.clearColor()
return cell
}
I have add the link of image which shows in blue color but when i try to click on image then it does not click and also not call the delegate
method but when i create the demo then it works fine.