Pdfview水平/垂直滚动在一个视图中显示2页

时间:2017-12-08 10:57:14

标签: objective-c ios11 pdfkit pdfview

我已将pdfview滚动方向设置为水平/垂直以水平/垂直滚动pdf。但是在两种情况下都会出现在一个视图中总是出现两个页面(第一页获得80%的屏幕而第二页占据屏幕的20%)。如果我使用usePageViewController,那么只有它在视图中出现一个页面,但如果我使用pageviewcontroller,则高亮显示文本功能不起作用。这是代码。

#import <PDFKit/PDFKit.h>

@interface ViewController ()<PDFDocumentDelegate,PDFViewDelegate>
{
    PDFView *pdfView;
    PDFPage *page;
    NSMutableArray *arrHighlitedSelections;
}
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    arrHighlitedSelections = [NSMutableArray new];

    pdfView = [[PDFView alloc] initWithFrame: self.view.bounds];

    pdfView.maxScaleFactor = 4.0;
    pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit;
    pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
    pdfView.displayMode = kPDFDisplaySinglePageContinuous;
    pdfView.displaysRTL = YES ;
    [pdfView usePageViewController:YES withViewOptions:nil];
    [pdfView setDisplaysPageBreaks:YES];
    pdfView.delegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionChangedNotification:) name:PDFViewSelectionChangedNotification  object:nil];

    [self.view addSubview:pdfView];

    NSURL * URL = [[NSBundle mainBundle] URLForResource: @"sample" withExtension: @"pdf"];

    PDFDocument *document = [[PDFDocument alloc] initWithURL: URL];
    pdfView.document = document;

    UITapGestureRecognizer *pdfVwTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearHighlightGesture:)];
    [pdfView addGestureRecognizer:pdfVwTapRecognizer];
}


#pragma mark - Highlight Texts

- (void)highLight:(id) sender {
    PDFSelection *slc = pdfView.currentSelection;
    NSArray *arrSelections = slc.selectionsByLine;
    [arrHighlitedSelections addObject:arrSelections];

    NSMutableArray *arr = [NSMutableArray new];

    for (int  i = 0; i<[arrHighlitedSelections count]; i++) {
            for (PDFSelection *sl in arrHighlitedSelections[i]) {
                slc.color = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0.0/255.0 alpha:0.6];
                [arr addObject:sl];
        }
    }

    [pdfView setHighlightedSelections:arr];
}

#pragma mark - Selection Changed Notification

- (void) selectionChangedNotification:(NSNotification *) notification
{
    PDFView *pdfVw = notification.object;
    NSLog(@"selected text: %@",pdfVw.currentSelection.string);

    UIMenuItem *highlight = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(highLight:)];

    [[UIMenuController sharedMenuController] setMenuItems:@[highlight]];
} 

如何在PDFKit中的单个视图中放置一个pdf页面?

2 个答案:

答案 0 :(得分:0)

尝试设置此部分

pdfView.maxScaleFactor = 4.0;
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit;
设置文档后

PDFDocument *document = [[PDFDocument alloc] initWithURL: URL];
pdfView.document = document;

pdfView

上设置文档后,也许还会尝试设置其他相关属性

答案 1 :(得分:0)

试试这个

pdfiew.displayMode = kPDFDisplaySinglePageContinuous;
pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
[pdfView usePageViewController:YES withViewOptions:nil];
 self.pdfView.autoScales = YES;
pdfView.minScaleFactor = self.pView.scaleFactorForSizeToFit;