Objective c UILabel或UITextView提供的智能标题

Objective c UILabel或UITextView提供的智能标题,objective-c,uitextfield,uilabel,uitextview,sizetofit,Objective C,Uitextfield,Uilabel,Uitextview,Sizetofit,我想像SoundCloud应用程序一样创建一个智能标题。 见下面的附件 这两个标题: -•潘• -德国柏林 是我想要创造的 这些标题似乎是由sizeToFit或sizeThatFits执行的。但是,如果将sizeThatFits与背景色(由NSBackgroundColorAttributeName提供)配合使用,则不会同时获得第一个字母之前和最后一个字母之后的填充以及顶部和底部填充。 标题将按照这些字母的准确尺寸进行组织 无论如何,我想做的是完全相同的标题像附件图片 干杯,这里有两种方法。

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

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

是我想要创造的

这些标题似乎是由sizeToFit或sizeThatFits执行的。但是,如果将sizeThatFits与背景色(由NSBackgroundColorAttributeName提供)配合使用,则不会同时获得第一个字母之前和最后一个字母之后的填充以及顶部和底部填充。 标题将按照这些字母的准确尺寸进行组织

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


干杯,

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


但是,添加填充的正确方法是扩展
UILabel
,并在子类上重写方法
textRectForBounds:limitedToNumberOfLines:
。在这里,只需在
super
上调用相同的方法,通过您收到的
边界,只需更小。

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


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

我知道这是一个老问题,但我一直在寻找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()

我知道这是一个老问题,但我一直在寻找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()

谢谢你的建议。我将尝试根据您的流程进行测试。会让你知道如果工作!如果您不介意,请与我分享一段示例代码?谢谢您的建议。我将尝试根据您的流程进行测试。会让你知道如果工作!如果您不介意,请与我分享一段示例代码?谢谢您的建议。我将尝试根据您的流程进行测试。会让你知道如果工作!如果你不介意的话,可以和我分享一段示例代码吗?