请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何根据框架的大小分隔 UIScrollView/UITextView 的字符串

[复制链接]
菜鸟教程小白 发表于 2022-12-13 17:22:21 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我目前正在尝试读取 RSS 提要并将长段落文本分离到启用分页的 UIScrollView 中。我需要将文本分成不同的页面,也就是适合每个页面的内容并将字符串分开。我不确定这样做是否有标准,我认为这是大多数 RSS 阅读应用程序在多个页面上分离信息的方式。有谁知道如何解决这个问题?在文本不适合并继续之前,我不想逐个字母地查看。

编辑:

这是一个好的开始,但是示例代码几乎遇到了我试图避免并且不知道如何解决的问题。此范围为 UITextView 计算不正确。我更改了字体,如下所示。一切都试图在 - (NSRange)visibleRangeOfTextViewUITextView *)textView 内计算。此方法由 -(void)adjustTextDisplay 调用,该方法在为 UITextView 设置文本后由外部类调用。我不知道为什么将内容大小设置为屏幕的框架大小不会限制 View (如下所示),也不知道为什么此方法将完整的字符串长度作为范围返回。



Best Answer-推荐答案


我将完整的(当然从上一页开始)文本设置为 textView 和 get the last displayed character position,而不是繁重的迭代计算。 .然后很容易执行快速向后搜索以截断单词/句子。


我有以下解决方案,跳过最后部分显示的行以避免滚动并使其看起来更好看,这是一个有点棘手的部分。您仍然需要移动 endCutIndex 以使其自动换行。

带有寻呼机但没有 TextView 的基础项目取自 here

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString * fullText = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";

    pageControlBeingUsed = NO;


    int pageNumber = 0;
    UIFont * theFont = [UIFont boldSystemFontOfSize:30];
    const CGSize charSize = [@"A" sizeWithFont:theFont];

    while (fullText.length > 0) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * (pageNumber++);
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];

        UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        textView.font = theFont;
        [subview addSubview:textView];
        [textView release];


        textView.text = fullText;

        CGRect bounds = textView.bounds;
        // - charSize.height to skip a partially visible line
        // - charSize.width*2 to skip annoying character still displayed at partially visible line
        CGPoint endPoint = CGPointMake(CGRectGetMaxX(bounds) - charSize.width*2, CGRectGetMaxY(bounds) - charSize.height);
        UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start;
        UITextPosition *end = [textView characterRangeAtPoint:endPoint].end;

        const int startCutIndex = [textView offsetFromPosition:textView.beginningOfDocument toPosition:start];
        const int endCutIndex =   [textView offsetFromPosition:textView.beginningOfDocument toPosition:end];

        NSString * cutText = [fullText substringToIndex:endCutIndex];
        textView.text = cutText;
        fullText = [fullText substringFromIndex:endCutIndex];

        [self.scrollView addSubview:subview];
        [subview release];

        NSLog(@"age (1-total) %d, start text index %d, end text index %d \ntext:%@", pageNumber, startCutIndex, endCutIndex, cutText);
    }

    const int totalPages = pageNumber;

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * totalPages, self.scrollView.frame.size.height);

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = totalPages;
}

这是 .h 文件的一部分:

@interface FCContentViewController : UIViewController <UIScrollViewDelegate, UITextViewDelegate>{
    UIPageControl *pageControl;   
    NSString *trunkedString;
    UITextView *bodyText;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSString *bodyTextString;

关于ios - 如何根据框架的大小分隔 UIScrollView/UITextView 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021253/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap