1.动态计算字符串所占区域
func heightForComment(font: UIFont, width: CGFloat) -> CGFloat {
let rect = NSString(string: comment).boundingRectWithSize(CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(rect.height)
}
2.计算一个图片放在另一个 view 按照一定的比例定宽动态取高
let photo = photos[indexPath.item]
let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
let rect = AVMakeRectWithAspectRatioInsideRect(photo.image.size, boundingRect)
3.利用map函数NSArray转Array<String>
let filePath = NSBundle.mainBundle().pathForResource("citydict", ofType: "plist")
let cityDict = NSDictionary(contentsOfFile: filePath!)
let keys = cityDict.allKeys.map({ object -> String in
return object as! String
}).sort()
4.UIImageJPEGRepresentation压缩图片
extension UIImage {
var uncompressedPNGData: NSData { return UIImagePNGRepresentation(self)! }
var highestQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 1.0)! }
var highQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.75)! }
var mediumQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.5)! }
var lowQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.25)! }
var lowestQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 0.0)! }
}
5.修改图片大小
extension UIImage{
func resizeImage(newSize:CGSize)->UIImage{
guard self.size != size else { return self }
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
6.UIBezierPath绘制圆形
center:弧线圆心坐标
radius:弧线半径
**startAngle:
**弧线起始角度
**endAngle:
**弧线结束角度
clockwise:是否顺时针绘制
path.addArcWithCenter(CGPoint(x: size.width / 2, y: size.height / 2),
radius:size.width / 2,
startAngle:CGFloat(-3 * M_PI_4),
endAngle:CGFloat(-M_PI_4),
clockwise:true)
7.代码单独设置view宽高约束 方便利用来动态改变view的宽高
let heightContraints = NSLayoutConstraint(item: customView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 70) let widthContraints = NSLayoutConstraint(item: customView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 70)
网友评论