I see somewhere this code for highlight selected text in pdf document:
let select = pdfView.currentSelection?.selectionsByLine()
//assuming for single-page pdf.
guard let page = select?.first?.pages.first else { return }
select?.forEach({ selection in
let highlight = PDFAnnotation(bounds: select.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.orange.withAlphaComponent(0.5)
page.addAnnotation(highlight)
})
for me ,
let select = pdfView.currentSelection?.selectionsByLine()
is always giving nil. where to put this code. I putting it in Gesture Recogniser Delegate methods.
Also it seems adding multiple annotation on page. Do we get single highlight annotation per selection on a page?
Related
I use PDFKit in iOS as PDF renderer and I have problem with background color in radio button.
I iterate all PDF annotations on all pages in the document.
override func viewDidAppear(_ animated: Bool) {
//let pdfData = ... received data from file storage ...
if let document = PDFDocument(data: pdfData) {
for i in 0...document.pageCount {
if let page = document.page(at: i) {
for annot in page.annotations {
if annot.widgetControlType == .radioButtonControl {
annot.backgroundColor = UIColor.red
}
}
}
}
}
}
When I set the background color, nothing happens.
But when I try to set the background color to the type of Text annotation, the color changes.
The difference between the Radio button and the Text annotation is in its type.
The Radio button has a widgetFieldType == .button and the Text has a widgetFieldType == .text.
I think this is the reason why it doesn't work.
Next I try to remove annotation, change his background color and add again. This also doesn't work.
if annot.widgetControlType == .radioButtonControl {
page.removeAnnotation(annot)
annot.backgroundColor = UIColor.red
page.addAnnotation(annot)
}
But when I create a new instance of PDFAnnotaion, and I add it to the page, it works.
if annot.widgetControlType == .radioButtonControl {
page.removeAnnotation(annot)
let newAnnot = PDFAnnotation(bounds: annot.bounds, forType: .widget, withProperties: nil)
newAnnot.backgroundColor = UIColor.red
newAnnot.widgetStringValue = annot.widgetStringValue
page.addAnnotation(newAnnot)
}
The big problem is that the value of this Radio button is not displayed, even though it contains the value itself.
If in another method I get all the values from PDF annotations, there is also the right value from this radio button.
The Radio button contains the value, only it is displayed.
I tried to copy all the properties PDF annotation from the annot to newAnnot, but it didn't help.
How to properly change the background color of the Radio button and display its value?
I am trying to include a highlighting function within a pdfViewer. However, in order to add the highlight annotation, I need the bounds for the selected text as a CGRect. Is there any way I can obtain this?
let annotation = PDFAnnotation(bounds: bounds, forType: .highlight, withProperties: nil)
Get an array of selections where each selection corresponds to a single line of the selected text:
guard let selections = pdfView.currentSelection?.selectionsByLine()
else { return }
Loop over the selections line by line, then loop over the pages encompassed by each selection, Then create a new highlight annotation with the selection's bounds and add it to the page
selections.forEach({ selection in
selection.pages.forEach({ page in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.color = .yellow
page.addAnnotation(highlight)
})
})
I am a new iOS programming. Now i am creating a sample app which display text using ReadMoreTextView library. My content may contain many lines but by using this library i can maximumNumberOfLines to display how many lines of content should be displayed. I implement those content in cell of UITableView and i have problem is that, when i use label.attributedReadMoreText = NSAttributedString(string: "...") then end of content will display ... and when i click on it and then whole content will be display so, my question is that: How to not letting user click on that ... because i want user to click on cell then i will show another view and display whole content there?
How can i achieve something like this? Thank in advance.
This is how i set UITextView
lazy var categoryShortDetailLabel: ReadMoreTextView = {
let label = ReadMoreTextView()
label.font = UIFont(name: "SFCompactText-Regular", size: 16)
label.textColor = .black
label.isEditable = false
label.isSelectable = false
label.maximumNumberOfLines = 3
label.shouldTrim = true
label.attributedReadMoreText = NSAttributedString(string: "...")
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
Looking at the code here, I found that, the ReadMoreTextView is meant to provide you the feature like, ReadMore and ReadLess for the larger texts in textView.
However your requirement is to stop that functionality. Now, if you take a look at the code here, you will get the idea, that the function shoreMoreText and it's a private function so, can't override it. and this function is expanding the texts and setting the numberOfLines to zero. so, what you can do is, comment the code within and return from function to stop doing the action. Also as the ReadMoreTextView is Licensed as MIT(Read licence here) so, it's okay to modify the code.
private func showMoreText() {
return
/*if let readLessText = readLessText, text.hasSuffix(readLessText) { return }
shouldTrim = false
textContainer.maximumNumberOfLines = 0
if let originalAttributedText = _originalAttributedText?.mutableCopy() as? NSMutableAttributedString {
attributedText = _originalAttributedText
let range = NSRange(location: 0, length: text.count)
if let attributedReadLessText = attributedReadLessText {
originalAttributedText.append(attributedReadLessText)
}
textStorage.replaceCharacters(in: range, with: originalAttributedText)
}
invalidateIntrinsicContentSize()
invokeOnSizeChangeIfNeeded()*/
}
Try and share your results.
Hope it helps!
Is there any helpful walkthrough on how to use the pdfkit annotations(highlight, comments, etc..) and save the results in swift.
I couldn’t find a helpful one.
Thank you
For Highlight you can use following code :
let selections = self.pdfview.currentSelection?.selectionsByLine()
// Simple scenario, assuming your pdf is single-page.
guard let page = selections?.first?.pages.first else { return }
if selections == nil {
}else {
selections?.forEach({ selection in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
// highlight.endLineStyle = .square
highlight.color = UIColor(red:0.49, green:0.99, blue:0.00, alpha:1.0).withAlphaComponent(0.5)
page.addAnnotation(highlight)
})
}
For Save :
self.pdfdocument?.write(to: urlPath!)
Here is what I am doing currently.
First, I get the document. Next I create a PDF View and assign the document to the view. Then I create a view and do my drawing in the view, then add the view to the PDFView as a subview. Then I convert the PDFView to data, then to PDF document. Since I'm doing the drawing on the first page of the PDF, I remove the first page from the original PDF, insert the new first page, and return the document.
guard let document = PDFDocument(url: url) else { return nil }
guard let page = document.page(at: 0) else { return nil }
let pageBounds = page.bounds(for: .mediaBox)
let pdfView = PDFView(frame: pageBounds)
pdfView.document = document
pdfView.enclosingScrollView?.autohidesScrollers = true
let view = /// code for subview that I am drawing on
pdfView.addSubview(view)
let data = pdfView.dataWithPDF(inside: pageBounds)
guard let pdf = PDFDocument(data: data) else { return nil }
document.removePage(at: 0)
document.insert(pdf.page(at: 0)!, at: 0)
return document
Is there a better way to do this? To add a wrinkle, my final product has a weird scroll bar image (see Screenshot). I tried adding auto hide scrollers & enclosingScrollView?.hasVerticalScroller = false but neither seem to hide the scroll bar.
Thanks in advance!
So I've solved my own problem. For anyone else stuck like me, here is what I did. For example to draw a box on a page:
create a CGContext (ctx) for PDFDocuments. You can do this either with data or with a URL you want to write to.
create a CGPDFDocument with the document you want to edit
get the CGPage of the CGPDF you want to edit (cgPage)
and:
ctx?.beginPDFPage(nil)
ctx?.drawPDFPage(cgPage)
ctx?.beginPath()
let path = CGPath(roundedRect: box as CGRect, cornerWidth: 5, cornerHeight: 5, transform: nil)
ctx?.setStrokeColor(CGColor.black)
ctx?.setFillColor(color.cgColor)
ctx?.setLineWidth(10.0)
ctx?.addPath(path)
ctx?.strokePath()
ctx?.addPath(path)
ctx?.fillPath()
ctx?.endPDFPage()
ctx?.closePDF()
(If you created the context with a URL, close will write to disk. Otherwise you'll have to do something with the PDF data.)
A simple solution is demonstrated in the 2017 WWDC video on PDFKit. I assume you have an existing document to which you want to add some drawing. Here's what to do:
Give the document a delegate.
In the delegate, implement classForPage to declare a class for your pages: that class should be your own PDFPage subclass.
In your PDFPage subclass, implement draw(with:to:). You are given a context; draw into it! Be sure to call super if you want the default drawing to happen.
If your PDFPage subclass needs to draw different things on different pages, it can find out where it is in the document by asking for self.document?.index(for:self).