iOS在形状的中心绘制文本

我试图在iOS中的形状中心绘制文本,例如Microsoft Office的插入形状请参阅: https : //www.dropbox.com/s/cgqyyuvy6hcv5g8/Screenshot%202014-01-21%2013.48.17巴纽

我有一个用于形成形状的坐标数组,这对创build实际的形状工作正常。

我的第一个策略是遵循这个stackoverflow问题: iOS上的不对称形状的核心文本,但我只能得到它在形状的底部绘制文本。 有什么方法可以垂直和水平居中文本?

然后我看了一下新的TextKit,但是我被困在如何inheritanceNSTextContainer接受一个CGPath或坐标数组来创build文本容器绘制里面。

我的备份解决scheme是使用“核心文本”在形状的中心坐标处绘制文本,但这会使文本在一些形状(如直angular三angular形)上绘制到形状外部。

另外,还有什么其他的方法可以用来在一个形状的中心有点文字?

谢谢,

丹妮尔。

我从这些答案和相关问题中挑选出来 – 没有一个是真正令人满意的 – 并提出了这个简单的解决scheme:

UIGraphicsBeginImageContext(CGSize.init(width: imageWidth, height: imageHeight)) let string = "ABC" as NSString let attributes = [ NSFontAttributeName : UIFont.systemFontOfSize(40), NSForegroundColorAttributeName : UIColor.redColor() ] // Get the width and height that the text will occupy. let stringSize = string.sizeWithAttributes(attributes) // Center a rect inside of the image // by going half the difference to the right and down. string.drawInRect( CGRectMake( (imageWidth - stringSize.width) / 2, (imageHeight - stringSize.height) / 2, stringSize.width, stringSize.height ), withAttributes: attributes ) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() 

结果(从不知道颜色):

以图像为中心的字符串ABC

这有点棘手。 首先你必须创build一个上下文。 (bounds.size是图像的大小)

 UIGraphicsBeginImageContext(bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); 

然后得到文本的边框:(字体是你想要使用的字体)

 CGSize textSize = [yourText sizeWithAttributes:@{NSFontAttributeName:font}]; 

将文本绘制到上下文中(x和y标记图像的中心)

 [yourText drawAtPoint:CGPointMake(x, y) withAttributes:@{NSFontAttributeName:font}]; 

获取图像

 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

结束图像上下文

 UIGraphicsEndImageContext(); 

我还没有testing过,但这是要走的路。

Swift 3版本的Luca Davanzo的回答

 struct UIUtility { static func imageWithColor(color: UIColor, circular: Bool) -> UIImage? { let size: CGFloat = circular ? 100 : 1; let rect = CGRect(x: 0.0, y: 0.0, width: size, height: size) UIGraphicsBeginImageContext(rect.size) if let context = UIGraphicsGetCurrentContext() { context.setFillColor(color.cgColor) if(circular) { context.fillEllipse(in: rect) } else { context.fill(rect) } let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } return nil } static func drawText(text: NSString, font: UIFont, image: UIImage, point: CGPoint, textColor: UIColor = UIColor.white) -> UIImage? { UIGraphicsBeginImageContext(image.size) image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) let rect = CGRect(x: point.x, y: point.y, width: image.size.width, height: image.size.height) text.draw(in: rect.integral, withAttributes: [ NSFontAttributeName: font, NSForegroundColorAttributeName: textColor ]) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } static func circleImageWithText(text: String, font: UIFont, circleColor: UIColor, textColor: UIColor = UIColor.white) -> UIImage? { if let image = UIUtility.imageWithColor(color: circleColor, circular: true) { let textSize = NSString(string: text).size(attributes: [ 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 = CGPoint(x: centerX, y: centerY) return UIUtility.drawText(text: text as NSString, font: font, image: image, point: middlePoint) } return nil } } 

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) } 

}

有了这个工具,我们可以很容易地创build一个这样的形象:

 let font = UIFont() let image = UIUtility.circleImageWithText(text: "Center text", font: font, circleColor: UIColor.blackColor())