如何根据swift中的标签高度制作可伸缩的图像

时间:2014-12-08 10:43:26

标签: ios swift

我有Objective-c代码,但我想要快速代码。请建议我一个快速的代码。我已经尝试了所有的方法,但无法找到正确的答案。

1 个答案:

答案 0 :(得分:1)

嘿伙伴请找下面的代码,如果它可以帮助你

var lbl = UILabel(frame: CGRectMake(8,8, 240, CGFloat.max))
    lbl.numberOfLines = 0
    lbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
    lbl.font = UIFont(name: "Helvetica", size: 16.0)
    lbl.text = "Sample text"
    lbl.sizeToFit()

    let imageContainerMessage = UIImage(named: "balloon_unread_left.png")!.stretchableImageWithLeftCapWidth(15, topCapHeight: 17 )
    let sizeMessageLabelMax = CGSizeMake(240, 9999)
    let expectedSizeMessage = lbl.sizeThatFits(sizeMessageLabelMax)
    lbl.frame = CGRect(origin: CGPoint(x: 15, y: 10), size: expectedSizeMessage)

    var imageContainer = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSizeMake(expectedSizeMessage.width + 25, expectedSizeMessage.height +  25)))
    imageContainer.image = imageContainerMessage

    imageContainer.addSubview(lbl)