Swift 2.0兼容。
我的目的是画一个圆形,里面有居中的文字。
struct UIUtility {
static func imageWithColor(color: UIColor, circular : Bool) -> UIImage {
let size : CGFloat = circular ? 100 : 1;
let rect = CGRectMake(0.0, 0.0, size, size)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
if(circular) {
CGContextFillEllipseInRect(context, rect)
}
else {
CGContextFillRect(context, rect)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
static func drawText(text: NSString, font: UIFont, image: UIImage, point: CGPoint, textColor: UIColor = UIColor.whiteColor()) -> UIImage {
UIGraphicsBeginImageContext(image.size)
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
let rect = CGRectMake(point.x, point.y, image.size.width, image.size.height)
text.drawInRect(CGRectIntegral(rect), withAttributes: [ NSFontAttributeName: font, NSForegroundColorAttributeName : textColor ])
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
static func circleImageWithText(text text: String, font: UIFont, circleColor: UIColor, textColor: UIColor = UIColor.whiteColor()) -> UIImage {
let image = UIUtility.imageWithColor(circleColor, circular: true)
let textSize = NSString(string: text).sizeWithAttributes([ NSFontAttributeName: font])
let centerX = (image.size.width / 2.0) - (textSize.width / 2.0)
let centerY = (image.size.height / 2.0) - (textSize.height / 2.0)
let middlePoint = CGPointMake(centerX, centerY)
return UIUtility.drawText(text, font: font, image: image, point: middlePoint)
}
}
使用此实用程序,我们可以通过以下方式轻松创建图像:
let font = UIFont()
let image = UIUtility.circleImageWithText(text: "Center text", font: font, circleColor: UIColor.blackColor())