原理就是将图片四周拉伸,用中间部分进行填充
//4.根据文字内容确定大小
let text:String = "今天成交量依然不能有效放有效放天成交量依然大,交量依然不能有效放天成交量依然不能有效放大,"
let btnBgImage:UIImage = UIImage(named: "sign_bg_tieshi")!
// 设置图片的拉伸方式
let w: CGFloat = btnBgImage.size.width * 0.5
let h: CGFloat = btnBgImage.size.height * 0.5
let newImage:UIImage = btnBgImage.resizableImageWithCapInsets(UIEdgeInsetsMake(h, w, h, w), resizingMode: .Stretch)
tipBtn.setBackgroundImage(newImage, forState: .Normal)
tipBtn.setTitle(text, forState: .Normal)
//根据字体的大小文字的长度算出rect
let texMaxSize: CGSize = CGSizeMake(220, CGFloat(MAXFLOAT))
let textRect = TKOnlineTool.getTextRectSize(text, font: UIFont.systemFontOfSize(13), size: texMaxSize)
let textHeight = textRect.size.height
tipBtnHeight.constant = textHeight + 30
/**
根据文字算出文字尺寸
*/
class func getTextRectSize(text:NSString,font:UIFont,size:CGSize) -> CGRect {
let attributes = [NSFontAttributeName: font]
let option = NSStringDrawingOptions.UsesLineFragmentOrigin
let rect:CGRect = text.boundingRectWithSize(size, options: option, attributes: attributes, context: nil)
// println("rect:\(rect)");
return rect;
}
网友评论