我必须从创建点击链接label
,所以我找到了这个链接https://github.com/d6u/TapLabel。当我创建演示时,它正在View
运行tableview
。但是当我在我的项目中使用这个TapLabel 示例时,它不起作用。实际上我需要在我的聊天视图中点击链接,我的视图看起来像一个信使。这是我的代码。该delegate
方法不起作用。
这是我的带有 Taplabel 委托方法的单元格
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)
}
}
**这是我的视图表格视图单元格**
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
}
我添加了以蓝色显示的图像链接,但是当我尝试单击图像时,它没有单击,也没有调用该delegate
方法,但是当我创建演示时,它可以正常工作。