本文介绍了iBooks App如何在单独的页面上格式化文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看iBooks应用程序,我想知道如何完成格式化文本(可能是一个非常简单的txt文件),这样它就不可滚动但分成不同的页面。

Looking at the iBooks App, I was wondering how it accomplishes to format text (probably a very simple txt file) so that it is NOT SCROLLABLE but divided on separate pages.

我想达到同样的效果,但只能使用可修改的文字。

I'd like to achieve the same, but only with editable text.

我需要从哪里开始? UITextView在滚动时不起作用。即使我将pagingEnabled属性设置为YES,它也不会执行该任务。

Where would I need to start? UITextView doesn't work as it scrolls. Even if I set the pagingEnabled property to YES, it won't do the job.

似乎我需要限制我可以放在某个页面上的文本数量。是否有限制UITextView输入的功能?我需要限制LINES(不是字符)的数量,因为它们将显示在完整的iPhone屏幕上(我猜你可以容纳20行左右,具体取决于字体大小)。

It seems as if I need to LIMIT the amount of text I can put on a certain page. Is there a function to LIMIT the input of a UITextView? I would need to limit the number of LINES (not characters) as they would be shown on a full iPhone screen (I guess you can fit 20 or so lines, depending on the font size).

任何帮助将不胜感激!由于我是初学者,我特别感谢使用类似方法的样本。

Any help would be appreciated! As I'm a beginner, I especially appreciate samples in which similar methods are put to use.

谢谢!

推荐答案

你需要的是一个自己的布局算法,它采用文本,测量它的大小并将其剪切,直到它适合单页文本视图。然后从文本的其余部分开始,对于下一个文本视图同样的事情,依此类推......在那之后(或算法内部),您可以在滚动视图上排列所有生成的文本视图(或者在数组中,您稍后翻阅分页动画 - 如果你喜欢俗气的话。我做了类似的事情,但是对于UILabels,它也应该与textviews一起使用。你需要的是: NSString的 - (CGSize)sizeWithFont :( UIFont *)font constrainedToSize:(CGSize)size 和r angeOfString:@options :NSBackwardsSearch (寻找单词空格)和 substringFromIndex: resp。 substringToIndex:

What you need is an own layout algorithm that takes the text, measuring its size and cuts it until it fits into a single page text view. Then start with the rest of the text, same thing for a next text view and so on... After that (or inside the algorithm) you arrange all the resulting text views on scroll view (or in an array, you later flip through with paging animations - if you like it cheesy). I did a similar thing, but with UILabels, it should also work with textviews. What you need is: NSString's - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size and rangeOfString:@" " options:NSBackwardsSearch (looking for word spaces) and substringFromIndex: resp. substringToIndex:

如果您需要更多信息,请发表评论。

If you need more information, just post a comment.

编辑

以下代码未经测试但包含您需要的大部分内容(希望如此),但可能会保留一些错误,特别是在递归时...我纠正了BackWardsSearch的想法,因为它可能需要花费很长时间来剪切长文本。我完全忽略了 - 这可能非常棘手 - 是在编辑时重新渲染。
但无论如何,这是代码。
它是一个视图控制器,假设为旧的4个成员(头文件未发布):

Hi following code is not tested but contains most of what you need (hopefully), but may hold some bugs, especially when it comes to the recursion... I corrected the BackWardsSearch idea, because it could take a looong time to cut a long text. What I totally ignored - and that could be really tricky - is re-rendering while editing.But anyway, here's the code.It's a view controller assumed to old 4 members (header file not posted):

  UIView *editableBook;
  NSMutableArray *content;
  int currentPage;
  UIFont *font;

这是控制器本身:

//
//  EditableBookController.m
//
//  Created by Kai on 09.03.11.
//

#import "EditableBookController.h"


@implementation EditableBookController

-(id)initWithText:(NSString *)text
{
  if (self=[super init])
  {
    font = [UIFont fontWithName:@"SomeFont" size:12];
    content = [[NSMutableArray alloc]init];
    [self cutInPages:text];
    currentPage = 0;
  }
  return self;
}

- (void)loadView
{
  self.view = [[UIView alloc] initWithFrame:CGRectMake(.0, .0, 768., 1024.)];//assuming portrait only ...
  editableBook = [[UIView alloc]initWithFrame:self.view.bounds];//could be a scroll view in alternate approach
  UISwipeGestureRecognizer *flipper = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextOrPrevious:)];
  [editableBook addGestureRecognizer:flipper];
  [flipper release];
  [self.view addSubview:editableBook];
  UITextView *textView = [[UITextView alloc]initWithText:[content objectAtIndex:currentPage]];
  textView.frame = editableBook.bounds;
  textView.font = font;
  textView.tag = 23;
  [editableBook addSubview:textView];
  [textView release];
}

-(void)nextOrPrevious:(id)sender
{
  UISwipeGestureRecognizer *flipper = (UISwipeGestureRecognizer*)sender;
  if(flipper.direction == UISwipeGestureRecognizerDirectionLeft)
  {
    [self next];
  }
  else if(flipper.direction == UISwipeGestureRecognizerDirectionRight)
  {
    [self previous];
  }
}

-(void)next
{
  if(currentPage == content.count - 1)
  {
    return;
  }
  currentPage++;
  UIView *fromView = [editableBook viewWithTag:23];
  UIView *toView =  [[UITextView alloc]initWithText:[content objectAtIndex:currentPage]];
  toView.frame = editableBook.bounds;
  toView.tag = 23;
  [UIView transitionWithView:editableBook duration:0.2 options:UIViewAnimationTransitionFlipFromRight
   animations:^
   {
     [fromView removeFromSuperview];
     [editableBook addSubview:toView];
   }
   completion:NULL];
}

-(void)previous
{
  if(currentPage == 0)
  {
    return;
  }
  currentPage--;
  UIView *fromView = [editableBook viewWithTag:23];
  UIView *toView =  [[UITextView alloc]initWithText:[content objectAtIndex:currentPage]];
  toView.frame = editableBook.bounds;
  toView.tag = 23;
  [UIView transitionWithView:editableBook duration:0.2 options:UIViewAnimationTransitionFlipFromLeft
   animations:^
   {
     [fromView removeFromSuperview];
     [editableBook addSubview:toView];
   }
   completion:NULL];
}

-(void)cutInPages:(NSString *)text
{
  NSRange whereToCut = whereToCut = [text rangeOfString:@" "];
  NSString *pageText = [text substringToIndex:whereToCut.location];
  NSString *rest = [text substringFromIndex:whereToCut.location];;
  CGFloat height = 0;
  while (height<1024.)
  {
    NSRange whereToCut = [rest rangeOfString:@" "];
    NSString *wordOfRest = [rest substringToIndex:whereToCut.location];
    pageText = [NSString stringWithFormat:@"%@%@", pageText, wordOfRest];
    rest = [rest substringFromIndex:whereToCut.location];;
    CGSize size = [pageText sizeWithFont:font
                      constrainedToSize:CGSizeMake(768., 10000)
                          lineBreakMode:UILineBreakModeWordWrap];
    height = size.height;
  }
  if(height>1024.)
  {
    //TODO cut the last word of pageText and prepend to the eest
  }
  [content addObject:pageText];
  if([rest length] > 0)
  {
    [self cutInPages:rest];
  }
}

- (void)dealloc
{
  [editableBook release];
  [content release];
  [super dealloc];
}


@end

这篇关于iBooks App如何在单独的页面上格式化文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:51
查看更多