CGContext renders text upside down - ios

I am rendering a NSAttributedString with CGContext, however when it renders the text is upside down :(
This is what I came up with:
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 80.0),
NSAttributedString.Key.foregroundColor: UIColor.red
]
NSAttributedString(string: self.widgetStringValue!, attributes: attributes).draw(in: self.bounds)
context.restoreGState()
UIGraphicsPopContext()
}
I have tried adding this:
context.translateBy(x: 0.0, y: bounds.height)
context.scaleBy(x: 1.0, y: -1.0)
But then the text does not appear on my screen at all :(
What am I doing wrong?
UPDATE
As requested here is my full TextAnnotation class:
class TextAnnotation: PDFAnnotation {
var currentText: String?
override init(bounds: CGRect, forType annotationType: PDFAnnotationSubtype, withProperties properties: [AnyHashable : Any]?) {
super.init(bounds: bounds, forType: annotationType, withProperties: properties)
self.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)
self.font = UIFont.systemFont(ofSize: 80)
self.isMultiline = true
self.widgetStringValue = "Text Here"
self.currentText = self.widgetStringValue!
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
let pageBounds = bounds
context.translateBy(x: 0, y: pageBounds.height)
context.scaleBy(x: 1, y: -1)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 80),
.foregroundColor: UIColor.red
]
widgetStringValue!.draw(in: pageBounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

In my case, I was subclassing a PDFAnnotation. The key was using the correct y-value (which was a pain to figure out) for the translateBy call in my draw method:
override func draw(with box: PDFDisplayBox, in context: CGContext) {
let text = NSString(string: "Hello!")
UIGraphicsPushContext(context)
context.saveGState()
context.translateBy(x: 0, y: bounds.height + (2 * bounds.minY))
context.scaleBy(x: 1, y: -1.0)
text.draw(in: bounds.offsetBy(dx: 2, dy: 0), withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}

In a PDFAnnotation, you should just add the translateBy(x:y:) and scaledBy(x:y:) as you’ve discussed:
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
context.translateBy(x: 0, y: bounds.height)
context.scaleBy(x: 1, y: -1)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 80),
.foregroundColor: UIColor.red
]
widgetStringValue?.draw(in: bounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
In a PDFPage you should use bounds(for:) to get the bounds within that PDFDisplayBox, and then use translateBy(x:y:) and scaledBy(x:y:) as you’ve discussed:
override func draw(with box: PDFDisplayBox, to context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
let pageBounds = bounds(for: box)
context.translateBy(x: 0, y: pageBounds.height)
context.scaleBy(x: 1, y: -1)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 80),
.foregroundColor: UIColor.red
]
text.draw(in: pageBounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}

Related

Position CGRect in the center for text output

So I have the following code:
let number = cluster.memberAnnotations.count
displayPriority = .defaultHigh
image = textToImage(
drawText: "\(number)",
inImage: UIImage(named: "map-pin-full-cluster-1")!
.withTintColor(.blue, renderingMode: . alwaysTemplate)
.resizeWith(
newSize: CGSize(width: 35, height: 35)), atPoint: CGPointMake(14.5, 3.5)
)
Which outputs the following look on the map:
Here is a function that I am using to assist me:
func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
] as [NSAttributedString.Key : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
Problem:
When the number ends up going into double digit, it isn't contained within the circle, how can I format my code so that I can center a number based on a CRect or other call?
:
Got it working thanks to #Larme's guidance:
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.paragraphStyle: paragraph,
] as [NSAttributedString.Key : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
Define a NSMutableParagraphStyle() and set the paragraphStyle to it's attribute.

How to draw vertical text on CGContext?

I want to draw a vertical text. I had tried different way to rotate string using rotate by but its not working
Here is my code
override func draw(with box: PDFDisplayBox, to context: CGContext) {
let name = "Sunny"
// Draw original content
super.draw(with: box, to: context)
// Draw rotated overlay string
UIGraphicsPushContext(context)
context.saveGState()
let pageBounds = self.bounds(for: box)
context.translateBy(x: pageBounds.size.width, y: 0)
context.scaleBy(x: -1.0, y: -1.0)
context.rotate(by: CGFloat.pi / 4.0)
let string: NSString = name as! NSString
let attributes = [
NSAttributedString.Key.foregroundColor: UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5),
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 64)
]
string.draw(at: CGPoint(x:250, y:40), withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
How can i draw?
We can draw the vertical string using the below code, in which I have converted given string by adding line separators between the characters.
override func draw(with box: PDFDisplayBox, to context: CGContext) {
// Draw original content
super.draw(with: box, to: context)
// Draw rotated overlay string
UIGraphicsPushContext(context)
context.saveGState()
let pageBounds = self.bounds(for: box)
context.translateBy(x: 0.0, y: pageBounds.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.rotate(by: CGFloat.pi / -60.0)
// Convert string by adding line separators.
let string = "Sunny".trimmingCharacters(in: .whitespaces)
let asArray = Array(string)
let verticalString = asArray.map { "\($0)" }.joined(separator: "\n")
let attributes: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.4980392157, green: 0.4980392157, blue: 0.4980392157, alpha: 0.5),
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 64)
]
verticalString.draw(at: CGPoint(x: 250, y: 40), withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
Output of vertical string in PDF

Text color in the HUD

Here is my code:
override func draw(_ rect: CGRect) {
let boxWidth: CGFloat = 96
let boxHeight: CGFloat = 96
let boxRect = CGRect(x: round((bounds.size.width - boxWidth) / 2),
y: round((bounds.size.height - boxHeight) / 2),
width: boxWidth, height: boxHeight)
let roundedRect = UIBezierPath(roundedRect: boxRect, cornerRadius: 10)
UIColor(white: 0.3, alpha: 0.8).setFill()
roundedRect.fill()
if let image = UIImage(named: "Checkmark") {
let imagePoint = CGPoint(
x: center.x - round(image.size.width / 2),
y: center.y - round(image.size.height / 2) - boxHeight / 8)
image.draw(at: imagePoint)
}
let attribs = [ kCTFontAttributeName: UIFont.systemFont(ofSize: 66.0), kCTForegroundColorAttributeName: UIColor.white]
let textSize = text.size(withAttributes: attribs as [NSAttributedString.Key : Any])
let textPoint = CGPoint(
x: center.x - round(textSize.width / 2),
y: center.y - round(textSize.height / 2) + boxHeight / 4)
text.draw(at: textPoint, withAttributes: attribs as [NSAttributedString.Key : Any])
}
The text color in the HUD is black. Why and how can I change it to white ?
You should use NSAttributedString.Key instead of CoreText keys try to replace this:
let attribs = [ kCTFontAttributeName: UIFont.systemFont(ofSize: 66.0), kCTForegroundColorAttributeName: UIColor.white]
with this:
let attribs: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 66.0),
.foregroundColor: UIColor.green]
You can change it on this line
let attribs = [ kCTFontAttributeName: UIFont.systemFont(ofSize: 66.0), kCTForegroundColorAttributeName: UIColor.white.CGColor]
The foreground color of the text to which this attribute applies. The value associated with this attribute must be a CGColor object. Default value is black. Refer here
Try this:
let attribs = [NSAttributedString.Key.font.rawValue: UIFont.systemFont(ofSize: 66.0), NSAttributedString.Key.foregroundColor: UIColor.white] as! [String : Any]

NSAttributedString Shadow and Stroke on iOS?

When I use stroke and shadow, I get some sort of double-stroke. How can I fix this?
Playground Code:
import UIKit
var shadow = NSShadow()
shadow.shadowColor = UIColor.black
shadow.shadowOffset = CGSize(width: 0, height: 3)
class CustomLabel: UILabel {
override func drawText(in rect: CGRect) {
let attributes: [String: Any] = [NSStrokeWidthAttributeName: -2.0,
NSStrokeColorAttributeName: UIColor.black,
NSForegroundColorAttributeName: UIColor.white,
NSShadowAttributeName: shadow,
NSFontAttributeName: UIFont(name: "AvenirNext-Bold", size: 50)]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: attributes)
super.drawText(in: rect)
}
}
let label = CustomLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.backgroundColor = UIColor.orange
label.text = "Hello"
Result:
I figured it out. If I apply a shadow to the label's CALayer, and disable the background-color, it works as expected:
import UIKit
class CustomLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 3)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 0.0
}
override func drawText(in rect: CGRect) {
let attributes: [String: Any] = [NSStrokeWidthAttributeName: -2.0,
NSStrokeColorAttributeName: UIColor.black,
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "AvenirNext-Bold", size: 50)]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: attributes)
super.drawText(in: rect)
}
}
let label = CustomLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.text = "Hello"

How to use CoreText to replace UILabel in Swift?

Currently I am using UILabel to put texts on the screen:
var label = UILabel()
view.addSubView(label)
label.backgroundColor = UIColor.whiteColor()
label.numberOfLines = 2
label.font = UIFont.systemFontOfSize(20, weight: UIFontWeightMedium)
label.frame = CGRectMake(0, 0, 100, 50)
label.text = "Hi, this is some text..."
How to use CoreText to replace the above in Swift?
Following code solved my question:
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let context = UIGraphicsGetCurrentContext()
// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
let path = CGPathCreateMutable()
CGPathAddRect(path, nil, self.bounds)
let str = NSMutableAttributedString(string: "some text goes here...")
// set font color
str.addAttribute(kCTForegroundColorAttributeName as String, value:UIColor.blackColor() , range: NSMakeRange(0,str.length))
// set font name & size
let fontRef = UIFont.systemFontOfSize(20, weight: UIFontWeightMedium)
str.addAttribute(kCTFontAttributeName as String, value: fontRef, range:NSMakeRange(0, str.length))
let frameSetter = CTFramesetterCreateWithAttributedString(str)
let ctFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0,str.length), path, nil)
CTFrameDraw(ctFrame, context!)
}
Update for Swift 4.0:
override func draw(_ rect: CGRect) {
super.draw(rect)
// context allows you to manipulate the drawing context (i'm setup to draw or bail out)
guard let context: CGContext = UIGraphicsGetCurrentContext() else {
return
}
// Flip the coordinate system
context.textMatrix = CGAffineTransform.identity;
context.translateBy(x: 0, y: self.bounds.size.height);
context.scaleBy(x: 1.0, y: -1.0);
let path = CGMutablePath()
path.addRect(self.bounds)
let str = NSMutableAttributedString(string: "00:00:00")
// set font color
str.addAttribute(NSAttributedStringKey(rawValue: kCTForegroundColorAttributeName as String), value:UIColor.purple , range: NSMakeRange(0,str.length))
// set font name & size
let fontRef = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.bold)
str.addAttribute(NSAttributedStringKey(rawValue: kCTFontAttributeName as String), value: fontRef, range:NSMakeRange(0, str.length))
let frameSetter = CTFramesetterCreateWithAttributedString(str)
let ctFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0,str.length), path, nil)
CTFrameDraw(ctFrame, context)
}
Try this:
func drawText(context: CGContextRef, text: NSString, attributes: [String: AnyObject], x: CGFloat, y: CGFloat) -> CGSize {
let font = attributes[NSFontAttributeName] as UIFont
let attributedString = NSAttributedString(string: text, attributes: attributes)
let textSize = text.sizeWithAttributes(attributes)
// y: Add font.descender (its a negative value) to align the text at the baseline
let textPath = CGPathCreateWithRect(CGRect(x: x, y: y + font.descender, width: ceil(textSize.width), height: ceil(textSize.height)), nil)
let frameSetter = CTFramesetterCreateWithAttributedString(attributedString)
let frame = CTFramesetterCreateFrame(frameSetter, CFRange(location: 0, length: attributedString.length), textPath, nil)
CTFrameDraw(frame, context)
return textSize
}
And call like this:
var textAttributes: [String: AnyObject] = [
NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 1.0).CGColor,
NSFontAttributeName : UIFont.systemFontOfSize(17)
]
drawText(ctx, text: "Hello, World!", attributes: textAttributes, x: 50, y: 50)
Hope this works!
Updated for swift 3.0:
if let context = UIGraphicsGetCurrentContext(){
// Flip the coordinate system
context.textMatrix = CGAffineTransform.identity;
context.translateBy(x: 0, y: self.bounds.size.height);
context.scaleBy(x: 1.0, y: -1.0);
let path = CGMutablePath()
path.addRect(self.bounds)
let str = NSMutableAttributedString(string: "some text goes here...")
// set font color
str.addAttribute(kCTForegroundColorAttributeName as String, value:UIColor.black , range: NSMakeRange(0,str.length))
// set font name & size
let fontRef = UIFont.systemFont(ofSize: 20, weight: UIFontWeightMedium)
str.addAttribute(kCTFontAttributeName as String, value: fontRef, range:NSMakeRange(0, str.length))
let frameSetter = CTFramesetterCreateWithAttributedString(str)
let ctFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0,str.length), path, nil)
CTFrameDraw(ctFrame, context)
}

Resources