iphone - iBooks 应用程序如何格式化不同页面上的文本?

标签 iphone sdk uiwebview uitextview ibooks

看着 iBooks 应用程序,我想知道它如何实现文本格式(可能是一个非常简单的 txt 文件),以便它不可滚动,而是分为不同的页面。

我想实现同样的目标,但只能使用可编辑文本。

我需要从哪里开始? UITextView 滚动时不起作用。即使我将 pagingEnabled 属性设置为 YES,它也无法完成这项工作。

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

如有任何帮助,我们将不胜感激!由于我是初学者,我特别欣赏使用类似方法的示例。

谢谢!

最佳答案

您需要的是一种自己的布局算法,该算法获取文本,测量其大小并剪切它,直到它适合单页 TextView 。然后从文本的其余部分开始,对于下一个 TextView 也是如此......之后(或在算法内部)您将所有生成的 TextView 排列在 ScrollView 上(或在数组中,稍后您可以翻转)带有分页动画 - 如果你喜欢它俗气的话)。我做了类似的事情,但是对于 UILabels,它也应该适用于 TextView 。您需要的是: NSString's - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size 和 rangeOfString:@""options:NSBackwardsSearch (寻找单词空格)和 substringFromIndex: 分别。 substringToIndex:

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

编辑:

嗨,以下代码未经测试,但包含您需要的大部分内容(希望如此),但可能存在一些错误,特别是在递归方面......我纠正了 BackWardsSearch 的想法,因为它可能需要很长时间才能完成剪一个长文本。我完全忽略了——这可能真的很棘手——是编辑时重新渲染。 但无论如何,这是代码。 这是一个假设为老 4 名成员的 View Controller (未发布头文件):

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

这是 Controller 本身:

//
//  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

关于iphone - iBooks 应用程序如何格式化不同页面上的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5236177/

相关文章:

ios - 是否可以使用 GoogleMaps iOS SDK 获取 Google map 显示的地点信息?

java.lang.NoClassDefFoundError : how do I fix this error? 错误

带有 iPhone webview 的 Javascript 接口(interface)

iPhone SDK - 在 UIWebView 中添加一个 "Add to Home Screen"按钮

iphone - 如何使用performSelectorOnMainThread调用setNeedsDisplayInRect?

iphone - 如何使UIImageView加载图像,但URL在UILabel上?

Android Studio Emulator 卡住并始终加载

ios - iOS 上的所有浏览器都使用 WKWebview 还是 UIWebVIew?

iPhone 3GS OpenGL 奇怪的 bug

iphone - 由 .xib 或 .m 设计