最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[部分解决]Swift中如何让UITableViewCell中只给显示出来的文字的区域添加背景色

Swift crifan 2687浏览 0评论
折腾:
[记录]Swift实现类似于微信的聊天对话框窗口
期间,已经实现:
当前行,整个的添加了背景色:
 add backgroud color for UITableViewCell
现在需要:
对于UITableViewCell中显示出来的text的部分,添加背景色。
搜:
swift uitableviewcell background only showing  text
swift add background for showing  text
swift add background for only  text region
swift add background for only  text part
swift add background fit  text
参考:
搜:
swift view fit text
参考:
算了,再去解决:
还要继续去折腾,如何只给文字显示区域加背景色。
搜:
swift uilabel auto fit text
参考:
最后代码:
            let timestampLabel = UILabel(frame: CGRectMake(0, LabelPadding, UIScreen.mainScreen().bounds.size.width, TimeStampLabelHeight))
            timestampLabel.text = "昨天 20:08"
            //timestampLabel.autoresizingMask = [UIViewAutoresizing.FlexibleWidth]
            timestampLabel.textColor = UIColor.whiteColor()
            timestampLabel.backgroundColor = UIColor.darkGrayColor()
            //let mainScreenBounds = UIScreen.mainScreen().bounds
            //print("mainScreenBounds=\(mainScreenBounds)") //mainScreenBounds=(0.0, 0.0, 375.0, 667.0)
            //let halfScreenWidth = mainScreenBounds.width / 2
            //print("halfScreenWidth=\(halfScreenWidth)") //halfScreenWidth=187.5
            //timestampLabel.center.x = halfScreenWidth
            timestampLabel.center.x = 350
            timestampLabel.textAlignment = NSTextAlignment.Center
            timestampLabel.font = UIFont.boldSystemFontOfSize(10)
            //timestampLabel.numberOfLines = 0
            timestampLabel.sizeToFit()

            cell.contentView.addSubview(timestampLabel)
            cell.contentView.bringSubviewToFront(timestampLabel)

效果:

label auto fit text for tableviewcell
很是奇怪,为何x值设置为,此处的screen的宽度的一半187.5,无法设置为屏幕中间对齐。
所以暂时算是部分解决问题。

转载请注明:在路上 » [部分解决]Swift中如何让UITableViewCell中只给显示出来的文字的区域添加背景色

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.172 seconds, using 22.18MB memory