我正在尝试制作一些小型聊天应用,消息周围带有灰色背景:
问题在于灰色背景是一个奇怪的形状(例如我用红色绘制的形状),而不是一个简单的圆角矩形:
messageLbl.frame = CGRect(x: 0, y: 0, width: self.resultsScrollView.frame.size.width-94, height: CGFloat.greatestFiniteMagnitude+32)
messageLbl.backgroundColor = UIColor.groupTableViewBackground
messageLbl.lineBreakMode = NSLineBreakMode.byWordWrapping
messageLbl.textAlignment = NSTextAlignment.left
messageLbl.numberOfLines = 0
messageLbl.font = UIFont(name: "Helvetica Neuse", size: 17)
messageLbl.textColor = UIColor.black
messageLbl.text = self.messageArray[i]
messageLbl.sizeToFit()
messageLbl.layer.zPosition = 20
//messageLbl.frame.origin.x = (self.resultsScrollView.frame.size.width - self.messageX) - messageLbl.frame.size.width //--> value of 449.5 too much right
messageLbl.frame.origin.y = self.messageY
//messageLbl.frame.origin.y = 20
messageLbl.frame.origin.x = 40
self.resultsScrollView.addSubview(messageLbl)
self.messageY += messageLbl.frame.size.height + 30
let frameLbl:UILabel = UILabel()
frameLbl.frame.size = CGSize(width: messageLbl.frame.size.width, height: messageLbl.frame.size.height + 10)
//frameLbl.frame.origin.x = (self.resultsScrollView.frame.size.width - self.frameX) - frameLbl.frame.size.width
frameLbl.frame.origin.x = 20
frameLbl.frame.origin.y = self.frameY
frameLbl.backgroundColor = UIColor.groupTableViewBackground
frameLbl.layer.masksToBounds = true
frameLbl.layer.cornerRadius = 10
self.resultsScrollView.addSubview(frameLbl)
self.frameY += frameLbl.frame.size.height + 20
let img: UIImageView = UIImageView()
img.image = self.myImg
img.frame.size = CGSize(width: 34,height: 34)
//img.frame.origin.x = (self.resultsScrollView.frame.size.width - self.imgX) - img.frame.size.width
frameLbl.frame.origin.x = 70
img.frame.origin.y = self.imgY
img.layer.zPosition = 30
img.layer.cornerRadius = img.frame.size.width/2
img.clipsToBounds = true
self.resultsScrollView.addSubview(img)
self.imgY += frameLbl.frame.height + 20
self.resultsScrollView.contentSize = CGSize(width: theWidth, height: self.messageY)
答案 0 :(得分:0)
frameLbl.frame.origin.x = 70
此代码很奇怪。您可以将x移至40,然后查看结果。