折腾:
期间,已经实现:
当前行,整个的添加了背景色:
现在需要:
对于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)
效果:
很是奇怪,为何x值设置为,此处的screen的宽度的一半187.5,无法设置为屏幕中间对齐。
所以暂时算是部分解决问题。
转载请注明:在路上 » [部分解决]Swift中如何让UITableViewCell中只给显示出来的文字的区域添加背景色