swift2 如何在swift中给标签添加下边框作为子层?

7uhlpewt  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(194)

我尝试在其中一个标签下插入彩色缐条:

let label = UILabel(frame: CGRectMake(0, 0, 70, 40))
label.text = items[index - 1]
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.font = UIFont(name: "Helvetica", size: 15)
label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor
label.translatesAutoresizingMaskIntoConstraints = false

var sublayer = label.layer;
sublayer.backgroundColor = UIColor.blueColor().CGColor
sublayer.frame = CGRectMake(0, 0, label.frame.width, 1);
sublayer.borderColor = UIColor.blackColor().CGColor;
sublayer.borderWidth = 1;
self.layer.insertSublayer(sublayer, atIndex: 0)

self.addSubview(label)

如何正确设置框架,使标签下有一条彩色线?

izkcnapc

izkcnapc1#

最后使用uiview:

let lineView = UIView(frame: CGRectMake(0,
                      self.frame.height - 3,
                      label.frame.width,
                      3.0))

lineView.backgroundColor = UIColor.blackColor()
self.addSubview(lineView)

效果很好。

eivgtgni

eivgtgni2#

雨燕三:

let lineView = UIView(frame: CGRect(x: 0, y: self.frame.height - 3, width: frame.width, height: 3.0))
lineView.backgroundColor = UIColor.black
self.addSubview(lineView)

相关问题