【问题标题】:Xcode 8.3.3 - How can I make 2 rows in a label?Xcode 8.3.3 - 如何在标签中制作 2 行?
【发布时间】: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 而无需串联。

标签: ios swift uilabel


【解决方案1】:

您应该使用UILabelattributedText 属性来实现这种外观。

使用\n 在标签中插入新行。

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let bigAttr = [NSForegroundColorAttributeName: UIColor.white,
               NSFontAttributeName: UIFont.systemFont(ofSize: 60, weight: UIFontWeightLight),
               NSParagraphStyleAttributeName: paragraphStyle]
let smallAttr = [NSFontAttributeName: UIFont.systemFont(ofSize: 12, weight: UIFontWeightLight)]



let attributedString = NSMutableAttributedString(string: "\(speedInt)", attributes: bigAttr)
attributedString.append(NSAttributedString(string: "\nkm/h", attributes: smallAttr))

statusLabel.attributedText = attributedString

【讨论】:

    【解决方案2】:

    在要拆分字符串的位置添加一个 \n。 "30 \n kmh" 或者简单地添加两个标签。

    【讨论】:

    • 这应该在评论中
    【解决方案3】:

    您可以在 UI 的帮助下简单地在标签中添加 2 行。看看下面 snap

    如上所示,您可以通过自动换行设置换行符,只需添加您想要的整个标题即可。在你的情况下0公里/小时。

    然后将光标放在 k 上并按 ctrl + Enter。标题将出现在两行中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2021-05-19
      • 2017-12-28
      • 1970-01-01
      相关资源
      最近更新 更多