//Swift
func getStrW(str:String)->CGFloat{
// 计算字符串的宽度,高度
let font = UIFont.systemFontOfSize(10)
let attributes = [NSFontAttributeName:font]
let option = NSStringDrawingOptions.UsesLineFragmentOrigin
let rect:CGRect = str.boundingRectWithSize(CGSizeMake(320.0, 999.9), options: option, attributes: attributes, context: nil)
return rect.width
}
//js
/**
* w 宽
* l 字符串长度
*/
stringInit:function(str,w,l){
var ln = this.context.measureText(str).width
if(ln > w){
return str.substring(0,l)+"…";
}
return str;
},