1

我想像 SoundCloud 应用程序一样创建一个智能字幕。见下面的附件,

在此处输入图像描述

这两个标题: - •PAN• - 德国柏林

是我想要创造的。

这些字幕似乎是由 sizeToFit 或 sizeThatFits 执行的。但是,如果将 sizeThatFits 与背景颜色一起使用(通过 NSBackgroundColorAttributeName),您将不会在第一个字母之前和最后一个字母之后以及顶部和底部填充都获得填充。标题将按照这些字母的精确尺寸进行组织。

在此处输入图像描述

无论如何,我想做的是与附件图片完全相同的标题。

干杯,

4

2 回答 2

0

您在这里有两种方法。一种是在调用后添加一点填充sizeThatFits。另一种是在标签标题前添加空格。

但是,添加填充的正确方法是扩展UILabel并在您的子类上覆盖 method textRectForBounds:limitedToNumberOfLines:super在那里,只需在传递bounds您收到的时调用相同的方法,只是更小。

于 2015-06-06T11:53:32.973 回答
0

我知道这是一个老问题,但我一直在寻找 SoundCloud 在其标签上的相同效果。这是 uilabel 的子类

    import UIKit

    class LabelPine: UILabel {

override func drawTextInRect(rect: CGRect) {
    let insets = UIEdgeInsets.init(top: 5, left: 0, bottom: 5, right: 3)
    super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}

override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
    super.textRectForBounds(bounds, limitedToNumberOfLines: 0)

    return CGRectInset(self.attributedText!.boundingRectWithSize(CGSizeMake(999, 999), options: .UsesLineFragmentOrigin, context: nil), -5, -5)




    }
}

这是各个类的实现:

   labelNombre = LabelPine()
    labelNombre?.text = nombreUser
    labelNombre?.frame = CGRectMake(10, nombrePos.Yo, nombrePos.ancho, nombrePos.alto)
    labelNombre?.font = UIFont(name:"Hiragino Sans W3",size: 19)!
    labelNombre?.textAlignment = .Left
    labelNombre?.backgroundColor = UIColor(white: 1, alpha: 0.5)
    labelNombre?.textColor = colorBlentUIColor
    labelNombre?.sizeToFit()

    header?.addSubview(labelNombre!)

注意我调用 sizeToFit()。

于 2016-05-22T21:42:58.970 回答