Swift Auto布局以编程方式设置气泡图像中的图像上边距(CHAT BUBBLES)

时间:2015-11-30 10:10:33

标签: ios iphone swift autolayout

我用快速语言创建了一个聊天页面,每个工作都重新设置了一个问题,问题是如何设置最高保证金 Bubbles 这是我的Tableview代码并发送问题图像此条件" if(count(getMessage)> 0)" 对于标签(工作正常)否则部分是图像(需要上边距)

class BHBMessageCell: UITableViewCell {

    let headerImgView:UIImageView = UIImageView()
     let contentImgView:UIImageView = UIImageView()


    let contentLbl:UILabel = UILabel()

    let bubbleImgView:UIImageView = UIImageView()
    var delegate: AppDelegate!

    var message:BHBMessage?{//Construction of cell layout model based on message

        didSet{
            delegate = UIApplication.sharedApplication().delegate as! AppDelegate
            //When an incoming message initialize the view , first remove , and then add , to avoid repeating cell reuse time add view
            self.headerImgView.removeFromSuperview()
            self.contentLbl.removeFromSuperview()

            self.contentImgView.removeFromSuperview() // Image

            self.bubbleImgView.removeFromSuperview()
            self.contentView.addSubview(self.headerImgView)
            self.contentView.addSubview(self.bubbleImgView)


            //self.bubbleImgView.addSubview(self.contentLbl)// Add label

            var getMessage:String! = message?.content

             if(count(getMessage) > 0)
             {
                      self.bubbleImgView.addSubview(self.contentLbl)// Add label
             }else
             {
                self.bubbleImgView.addSubview(self.contentImgView)// Add image
            }



            //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")






            self.contentLbl.text = message?.content

            var getImageUrl_LastHalf:String! = message?.msg_images
            println(getImageUrl_LastHalf)

            var getImageUrl:String! = "http://nishant.chandwani.com/images/\(getImageUrl_LastHalf)"
           // println("getImageUrl == \(getImageUrl)")
            self.contentImgView.image = UIImage(named:"placeholder.png")!
            if(!getImageUrl_LastHalf.isEmpty)
            {
                ImageLoader.sharedLoader.imageForUrl(getImageUrl, completionHandler:{(image: UIImage?, url: String) in
                    self.contentImgView.image = image
                })


            }


             //cell.img_user.image = UIImage(named:"placeholder.png")!


            //Layout position
            //1.Remove the system adds the default autolayout, prevent conflict , and set the label alignment
            self.contentImgView.setTranslatesAutoresizingMaskIntoConstraints(false) // for image


            self.headerImgView.setTranslatesAutoresizingMaskIntoConstraints(false)
            self.contentLbl.setTranslatesAutoresizingMaskIntoConstraints(false)
            self.bubbleImgView.setTranslatesAutoresizingMaskIntoConstraints(false)
            self.contentLbl.textAlignment = message?.role != Role.Receive ? NSTextAlignment.Right : NSTextAlignment.Left
            self.contentLbl.numberOfLines = 0

            var viewsDictionary : Dictionary<String,AnyObject>
            //2.  Set constraints
            //let viewsDictionary = ["header": self.headerImgView, "content": self.contentLbl, "bubble": self.bubbleImgView]

            if(count(getMessage) > 0)
            {
               // Add label
                 viewsDictionary = ["header": self.headerImgView, "content": self.contentLbl, "bubble": self.bubbleImgView]
            }else
            {
                // Add image

                 viewsDictionary = ["header": self.headerImgView, "content": self.contentImgView, "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(count(getMessage) > 0)
            {
            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-|"
            }
            }
            else
            {
                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  =  "|-10-[content]-(>=5)-|"
                    content_constraint_V_Format  =  "V:|[content]-20-|"
                }
            }

            let header_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_H_Format, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
            let header_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_V_Format, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

            let bubble_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(bubble_constraint_H_Format, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
            let bubble_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(bubble_constraint_V_Format, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

            let content_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(content_constraint_H_Format, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
            let content_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(content_constraint_V_Format, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

            self.contentView.addConstraints(header_constraint_H as [AnyObject])
            self.contentView.addConstraints(header_constraint_V as [AnyObject])
            self.contentView.addConstraints(bubble_constraint_H as [AnyObject])
            self.contentView.addConstraints(bubble_constraint_V as [AnyObject])
            self.bubbleImgView.addConstraints(content_constraint_H as [AnyObject])
            self.bubbleImgView.addConstraints(content_constraint_V as [AnyObject])
        }
    }

}

以下是我的问题图片enter image description here请帮助我,请分享您宝贵的知识。

0 个答案:

没有答案