【发布时间】:2017-09-18 13:39:52
【问题描述】:
所以基本上,我已经创建了一个速度计,但现在我让它在一条线上输出“30km/h”。
我希望它看起来像在一条线上有 30 公里/小时。
如果有人知道如何让你走得越快越粗的线,那将是一个很大的帮助。
这是我的代码现在的样子:
let speed = (location.speed*3.6)
let speedInt: Int = Int(speed)
//statusLabel.backgroundColor = UIColor.red
//statusLabel.layer.cornerRadius = 10.0
//statusLabel.clipsToBounds = true
let statusLabel = UILabel()
let size:CGFloat = 70.0
statusLabel.textColor = UIColor.black
statusLabel.textAlignment = .center
statusLabel.font = UIFont.systemFont(ofSize: 13.0)
statusLabel.frame = CGRect(x : 172.0,y : 580.0,width : size, height : size)
statusLabel.layer.cornerRadius = size / 2
statusLabel.layer.borderWidth = 2.0
statusLabel.layer.backgroundColor = UIColor.white.cgColor
statusLabel.layer.borderColor = UIColor.init(colorLiteralRed: 14.0/255, green: 122.0/255, blue: 254.0/255, alpha: 1.0).cgColor
statusLabel.text = "\(speedInt) km/h"
【问题讨论】:
-
解决方案:如果您希望每行有不同的大小/字体/颜色,请使用两个
UILabel或一个NSAttributedString。如果不是,为什么不在速度和单位之间使用\n? -
我认为“84”和“km/h”标签有不同的字体。因此,在故事板中,只需制作两个标签。如果您正确设置约束,您将拥有您正在寻找的东西。通过这样做,您可以只使用
speedInt而无需串联。