MKMarkerAnnotationView title not getting drawn on a MKSnapShot image - swift

I'm trying to draw a marker on an image (MKSnapshot) with a title, but the title is not showing up.
Does anyone has any idea why this would be the case?
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "TitleTest"
let pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "test")
pinView.titleVisibility = MKFeatureVisibility.visible
pinView.dragState = .none
pinView.animatesWhenAdded = false
pinView.canShowCallout = false
pinView.titleVisibility = .visible
pinView.contentMode = .scaleAspectFit
pinView.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
if rect!.contains(point) {
let pinCenterOffset = pinView.centerOffset
point.x -= pinView.bounds.size.width / 2
point.y -= pinView.bounds.size.height / 2
point.x += pinCenterOffset.x
point.y += pinCenterOffset.y
}
pinView.drawHierarchy(in: CGRect(
x:point.x,
y:point.y,
width:pinView.bounds.width,
height:pinView.bounds.height),
afterScreenUpdates: true)
print("Draw a marker on iOS 11")
This is a screenshot of my UIImageView with the marker, but without the title:

The title can be drawn on the snapshot below the location of the pin:
determine the position of the annotation in the coordinate system of the image snapshot.point(for: annotation.coordinate)
create the attributes for title text, you can make it similar to what Apple uses:
private func titleAttributes() -> [NSAttributedStringKey: NSObject] {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let titleFont = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.semibold)
let attrs = [NSAttributedStringKey.font: titleFont,
NSAttributedStringKey.paragraphStyle: paragraphStyle]
return attrs
}
draw it right below the position of the annotation:
private func drawTitle(title: String,
at point: CGPoint,
attributes: [NSAttributedStringKey: NSObject]) {
let titleSize = title.size(withAttributes: attributes)
title.draw(with: CGRect(
x: point.x - titleSize.width / 2.0,
y: point.y + 1,
width: titleSize.width,
height: titleSize.height),
options: .usesLineFragmentOrigin,
attributes: attributes,
context: nil)
}
For a more complete example with code and a screenshot, see my following answer (it draws a custom pin image but the rest of the code can be used here).

Related

How can I properly pass an array in iOS PDFKit

I'm trying to in an [string] array for a PDF. Below is what I have so far. I am guess I need to do a foreach somewhere, but I'm not entirely sure.
I thought something like this might work, but it does not.
for entry in body {
let attributedText = NSAttributedString(
string: entry,
attributes: textAttributes
)
}
private func addBody(body: [String], pageRect: CGRect, textTop: CGFloat) {
let pageWidth = 8.5 * 72.0
let pageHeight = 11 * 72.0
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
let bodyCG = addInstructor(instructor: "", pageRect: pageRect)
let textFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .natural
paragraphStyle.lineBreakMode = .byWordWrapping
let textAttributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: textFont
]
let attributedText = NSAttributedString(
string: body,
attributes: textAttributes
)
let textRect = CGRect(
x: 15,
y: bodyCG + 30,
width: pageRect.width - 20,
height: pageRect.height - textTop - pageRect.height / 5.0
)
attributedText.draw(in: textRect)
}
Adding some additional details. If I don't use [String] and just use String everything works fine. The PDF is generated, The concept I'm struggling to understand is how can I pass an array for the PDF.
var courseAttendees : [String] = ["name", "name", "name", "name"]
For Example, I want to pass courseAttendees and then loop through the array the names are just overlapped and shown below.
Final code.
private func addBody(body: [String], textTop: CGFloat) {
let pageWidth = 8.5 * 72.0
let pageHeight = 11 * 72.0
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
let bodyCG = addInstructor(instructor: "", pageRect: pageRect)
let textFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)
let textAttributes: [NSAttributedString.Key: Any] =
[NSAttributedString.Key.font: textFont]
// keep track of the y position on the page. You might need
// to set this globally as you have multiple pages
var currentYPos: CGFloat = bodyCG
// Loop through the array
for entry in body {
let attributedText = NSAttributedString(
string: "\(entry)",
attributes: textAttributes
)
// Update the currentYPos
currentYPos += 15
// Use the currentYPos in the textRect
let textRect = CGRect(
x: 15,
y: currentYPos,
width: pageRect.width - 20,
height: pageRect.height - textTop - pageRect.height / 5.0
)
attributedText.draw(in: textRect)
}
}
Based on your question and image, I am assuming the PDF creation works fine but the data is not rendered as desired.
I think the two questions to answer here are:
Where to loop through your array
How to keep track of the current y coordinate in the page which is responsible for the vertical positioning
Here are some additions I made to try and fix your issue, I have added comments to what I have changed
// Somewhere appropriate in your code
var courseAttendees : [String] = ["name1", "name2", "name3", "name4"]
// Call the function, 15 is just a random number for textTop,
// give it what you feel is appropriate
addBody(body: courseAttendees, textTop: 15)
// I have removed the pageRect parameter since you create it
// in the function
private func addBody(body: [String], textTop: CGFloat) {
let pageWidth = 8.5 * 72.0
let pageHeight = 11 * 72.0
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
let bodyCG = addInstructor(instructor: "", pageRect: pageRect)
let textFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .natural
paragraphStyle.lineBreakMode = .byWordWrapping
let textAttributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: textFont
]
// keep track of the y position on the page. You might need
// to set this globally as you have multiple pages
var currentYPos: CGFloat = 0.0
// Loop through the array
for entry in body {
let attributedText = NSAttributedString(
string: entry,
attributes: textAttributes
)
// Update the currentYPos
currentYPos += bodyCG + 30
// Use the currentYPos in the textRect
let textRect = CGRect(
x: 15,
y: currentYPos,
width: pageRect.width - 20,
height: pageRect.height - textTop - pageRect.height / 5.0
)
attributedText.draw(in: textRect)
}
}
I have not tested the above so please give this a try and check if it solves your issue.
If not, comment and I will update this accordingly.

How to dispose from UIView subviews on one ViewController when navigating away from it

I'm starting to learn Swift and decided to build an app but without storyboard.
My SceneDelegate scene function instantiates a TabBarController
window = UIWindow(frame: UIScreen.main.bounds)
let tb = TabBarController()
window?.rootViewController = tb
window?.makeKeyAndVisible()
window?.windowScene = windowSceme
I have a TabBarController that extends from UITabBarController which pretty much styles the tab bar and sets it all up
For each item of the Tabbar I have a ViewController. For the purpose of this question I'm going to specify the first "Home".
In Home, which extends ViewController, I have the following
let homePageView = HomePageView()
override func viewWillLayoutSubviews() {
navigationController?.isNavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
homePageView.setupHomePage()
view.addSubview(homePageView)
}
override func viewWillDisappear(_ animated: Bool) {
homePageView.dispose()
}
The Controller is pretty much only in charge of calling whatever is going to be displayed on the screen within HomePageView(), the main component. This last, holds two more views. One carrousel HomePageCarrousel() and one header HomePageSocialUp(). This view instantiates the two latter referred to and sets the layout programmatically and adds them as subviews. It also includes a dispose function that sets the instantiated classes to nil such as --> Instantiate - homePageCarrousel = HomePageCarrousel() and the dispose function has homePageCarrousel = nil
That works perfectly fine but when I navigate away from the current view controller via the tab bar and navigate back to it, now I have two instances of HomePageCarrousel() and HomePageSocialUp() within HomeView
I'm probably holding a strong reference somewhere but I can't figure out when. Could someone point me to where should I look to debug it or what might be that is creating the issue.
I'm also providing the code for the two views duplicated in case the issue is there
HomePageSocialUp
class HomePageSocialUp: UIView {
let logo = UIImage(named: "LogoSmiles")
let socialImages: [UIImage] = [UIImage(named: "tripadvisor")!, UIImage(named: "instagram")!, UIImage(named: "facebook")!, UIImage(named: "linkedin")!]
func setupHeaderCircle() {
guard let insetTop = UIApplication.shared.keyWindow?.safeAreaInsets.top else {return}
let circlePath = UIBezierPath(arcCenter: CGPoint(x: UIScreen.main.bounds.width / 2, y: insetTop + 60), radius: CGFloat(90), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor.white.cgColor
layer.addSublayer(shapeLayer)
}
func setupSocialHeader() {
setupHeaderCircle()
layer.masksToBounds = true;
backgroundColor = UIColor.TintColor
layer.shadowColor = UIColor.lightGray.cgColor
layer.shadowOpacity = 0.8
layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
layer.shadowRadius = 3.0
layer.masksToBounds = false
frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 280)
let imageView = UIImageView()
guard let logo = logo else {return}
guard let insetTop = UIApplication.shared.keyWindow?.safeAreaInsets.top else {return}
imageView.frame = CGRect(x: CGFloat(Int((UIScreen.main.bounds.width - (logo.size.width)))) / 2, y: 120 - (logo.size.width / 2), width: logo.size.width, height: logo.size.height)
imageView.image = logo
addSubview(imageView)
}
}
HomePageCarrousel
class HomePageCarrousel: UIScrollView {
var images: [UIImage]?
var originX = 0
var numberOfIterations = 0
var timer: Timer?
func setupCarrousel() {
let x = (Int(UIScreen.main.bounds.width) - (Int(UIScreen.main.bounds.width) - 60)) / 2
frame = CGRect(x: x, y: (Int(frame.origin.y) - 350) / 2, width: Int(UIScreen.main.bounds.width) - 60, height: 350)
let newImage = textToImage(drawText: "Creating Smiles in unique places.", frame: frame, inImage: UIImage(named: "smiles1")!, atPoint: CGPoint(x: frame.origin.x + 20, y: frame.height - 20))
images = [newImage, UIImage(named: "smiles2")!]
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(startScrolling), userInfo: nil, repeats: true)
guard let imageCount = images?.count, let images = images else { return }
contentSize = CGSize(width: frame.width * CGFloat(images.count), height: frame.height)
for image in 0..<imageCount {
let imageView = UIImageView()
imageView.image = images[image]
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
let xPosition = frame.width * CGFloat(image)
imageView.frame = CGRect(x: xPosition, y: 0, width: frame.width, height: frame.height)
addSubview(imageView)
}
timer?.fire()
}
func textToImage(drawText text: String, frame: CGRect, 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))
text.draw(in: frame, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
#objc func startScrolling() {
print(originX)
guard let images = images else { return }
if originX == images.count {
originX = 0
numberOfIterations += 1
}
if numberOfIterations > 2 {
timer?.invalidate()
timer = nil
numberOfIterations = 0
}
let x = CGFloat(originX) * frame.size.width
setContentOffset(CGPoint(x: x, y: 0), animated: true)
originX += 1
}
}
Thanks upfront
Did you tried to remove the subviews, something like
homePageView.subviews.forEach { (view) in
//taking appropriate action to whichever view you want
view.removeFromSuperview()//for removing views
}

Swift 4 - NSGraphicsContext.setCurrent() has no member 'setCurrent'

So I am trying to overlay text onto a PDF document using a similar method to this answer.
I use a function to set the current Graphics Context, like it says to in the documentation.
let pdfPage: CGPDFPage = pdf.page(at: 1)!
//var pageRect = pdfPage.getBoxRect(CGPDFBox.mediaBox)
//print(pageRect)
let doc: PDFDocument = PDFDocument(url: pdfURL!)!
let page: PDFPage = doc.page(at: 0)!
var mediaBox: CGRect = page.bounds(for: .mediaBox)
let context = CGContext(destinationURL as CFURL, mediaBox: &mediaBox, nil)
let graphicsContext = NSGraphicsContext(cgContext: context!, flipped: false)
NSGraphicsContext.setCurrent(graphicsContext)
context!.beginPDFPage(nil)
page.draw(with: .mediaBox, to: context!)
let style = NSMutableParagraphStyle()
style.alignment = .center
let richText = NSAttributedString(string: "Hello, world!", attributes: [
NSAttributedStringKey.font: NSFont.systemFont(ofSize: 64),
NSAttributedStringKey.foregroundColor: NSColor.red,
NSAttributedStringKey.paragraphStyle: style
])
let richTextBounds = richText.size()
let point = CGPoint(x: mediaBox.midX - richTextBounds.width / 2, y: mediaBox.midY - richTextBounds.height / 2)
context!.saveGState()
do {
context!.translateBy(x: point.x, y: point.y)
context!.rotate(by: .pi / 5)
richText.draw(at: .zero)
}
context!.restoreGState()
context!.endPDFPage()
NSGraphicsContext.setCurrent(nil)
context?.closePDF()
}
And the line:
NSGraphicsContext.setCurrent(graphicsContext)
Throws an error that says "Type 'NSGraphicsContext' has no member 'setCurrent'"
Anyone have any ideas on what's going on? Is there something I'm missing in terms of a framework?
You need to use the current class property.
NSGraphicsContext.current = graphicsContext

Put CAShapeLayer on top of UIImage in UIBarItem

Background
I followed this tutorial to achieve custom badges:
http://www.stefanovettor.com/2016/04/30/adding-badge-uibarbuttonitem/
Problem
The badge layer is placed underneath the UITabBarItem image. How do I place it on top of the image? I tried to insert the badge layer last with view.layer.insertSublayer but no luck.
func addBadge(number number: Int, withOffset offset: CGPoint = CGPoint.zero, andColor color: UIColor = UIColor.redColor(), andFilled filled: Bool = true) {
guard let view = self.valueForKey("view") as? UIView else { return }
// Initialize Badge
let badge = CAShapeLayer()
let radius = CGFloat(9)
let location = CGPoint(x: view.frame.width - (radius + offset.x), y: (radius + offset.y))
badge.drawCircleAtLocation(location, withRadius: radius, andColor: color, filled: filled)
view.layer.addSublayer(badge)
// Initialiaze Badge's label
let label = CATextLayer()
label.string = "\(number)"
label.alignmentMode = kCAAlignmentCenter
label.fontSize = 13
label.frame = CGRect(origin: CGPoint(x: location.x - 5, y: offset.y + 1), size: CGSize(width: 9, height: 16))
label.foregroundColor = filled ? UIColor.whiteColor().CGColor : color.CGColor
label.backgroundColor = UIColor.clearColor().CGColor
label.contentsScale = UIScreen.mainScreen().scale
badge.addSublayer(label)
}

UITableViewRowAction with image, swift

In my app I would like to use UITableViewRowAction with image instead of title text. I set background image using:
let edit = UITableViewRowAction(style: .Normal, title: "Edit") { action, index in
self.indexPath = indexPath
self.performSegueWithIdentifier("toEdit", sender: self)
}
edit.backgroundColor = UIColor(patternImage: UIImage(named: "edit")!)
However image appears many times.
How can I fix this to have only one image in row?
The problem is that the image used as pattern won't fit the space, It will be repeated in order to fill it.
One option to have a non-repeated image is to
use a UITableViewCell with fixed height
use image that fits that height
I have wrote a subclass of UITableViewRowAction to help you calculating the length of the title and you just pass the size of rowAction and the image.
class CustomRowAction: UITableViewRowAction {
init(size: CGSize, image: UIImage, bgColor: UIColor) {
super.init()
// calculate actual size & set title with spaces
let defaultTextPadding: CGFloat = 15
let defaultAttributes = [ NSFontAttributeName: UIFont.systemFont(ofSize: 18)] // system default rowAction text font
let oneSpaceWidth = NSString(string: " ").size(attributes: defaultAttributes).width
let titleWidth = size.width - defaultTextPadding * 2
let numOfSpace = Int(ceil(titleWidth / oneSpaceWidth))
let placeHolder = String(repeating: " ", count: numOfSpace)
let newWidth = (placeHolder as NSString).size(attributes: defaultAttributes).width + defaultTextPadding * 2
let newSize = CGSize(width: newWidth, height: size.height)
title = placeHolder
// set background with pattern image
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.nativeScale)
let context = UIGraphicsGetCurrentContext()!
context.setFillColor(bgColor.cgColor)
context.fill(CGRect(origin: .zero, size: newSize))
let originX = (newWidth - image.size.width) / 2
let originY = (size.height - image.size.height) / 2
image.draw(in: CGRect(x: originX, y: originY, width: image.size.width, height: image.size.height))
let patternImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
backgroundColor = UIColor(patternImage: patternImage)
}
}
You can see my project: CustomSwipeCell for more detail.