如何基于框架的大小分隔UIScrollView/UITextView的字符串 [英] How To Separate Strings For UIScrollView/UITextView based on the size of the frame

查看:89
本文介绍了如何基于框架的大小分隔UIScrollView/UITextView的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试阅读RSS提要,并将较长的文本分隔为具有分页功能的UIScrollView.我需要将文本分成不同的页面,也就是适合每个页面的内容,并像这样将字符串分开.我不确定是否有标准执行此操作,并且我假设这是大多数RSS阅读应用程序将其信息分散在多个页面上的方式.有谁知道如何解决这个问题?在文本不适合并继续之前,我不想逐个字母地看.

I'm currently trying to read in a RSS feed and separate the long paragraphed text into a UIScrollView with paging enabled. I need to separate the text into different pages aka fit what will fit on each page and separate the string as such. I'm not sure if there is a standard what of doing this and I assume that this is how most RSS reading apps separate their information on multiple pages. Does anyone know how to tackle this? I did not want to look letter by letter until the text didn't fit and continue.

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

This is a good start, but the example code pretty much runs into the problems I was trying to avoid and don't know how to get around. This range calculates incorrectly for the UITextView. I change the font and such as seen below. Everything is attempting to being calculated within - (NSRange)visibleRangeOfTextView:(UITextView *)textView. This method is called by -(void)adjustTextDisplay which is called by an external class after setting the text for the UITextView. I have no idea why setting the content size to the frame size of the screen does not restrict the view (as shown below) nor do I know why this method is returning the full string length as the range.

推荐答案

从iOS 7开始,使用TextKit对此有一个更优雅的解决方案,我已在下面的示例代码中提供了该解决方案.这个想法是让TextKit的布局管理器处理分离字形,并以这种方式正确地布局所有内容.这样可以避免在中途切断单词,并避免大量的灵活性:

As of iOS 7, there's a much more elegant solution to this using TextKit that I've included in sample code below. The idea is to let TextKit's layout manager handle separating the glyphs and lay everything out that way properly. This prevents cutting off words mid way and a ton of flexibility:

class BookView: UIScrollView {

    var bookMarkup: NSAttributedString!
    private let layoutManager = NSLayoutManager()

    override func layoutSubviews() {
        super.layoutSubviews()

        if layoutManager.textContainers.count == 0 {
            buildFrames()
        }
    }

    func buildFrames() {
        let textStorage = NSTextStorage(attributedString: bookMarkup)

        textStorage.addLayoutManager(layoutManager)

        var range = NSMakeRange(0, 0)
        var containerIndex = 0

        while NSMaxRange(range) < layoutManager.numberOfGlyphs {
            let textViewRect = frameForViewAtIndex(containerIndex)
            let containerSize = CGSizeMake(CGRectGetWidth(textViewRect), CGRectGetHeight(textViewRect) - 16) //UITextView adds an 8 margin above and below the container so we take that into consideration here with the 16. heightTracksTextView causes a performance hit when adding multiple containers... so we don't do that instead
            let textContainer = NSTextContainer(size: containerSize)
            layoutManager.addTextContainer(textContainer)

            let textView = UITextView(frame: textViewRect, textContainer: textContainer)

            addSubview(textView)

            containerIndex++

            range = layoutManager.glyphRangeForTextContainer(textContainer)
        }

        contentSize = CGSize(width: CGRectGetWidth(bounds) / 2 * CGFloat(containerIndex), height: CGRectGetHeight(bounds))

        pagingEnabled = true
    }

    private func frameForViewAtIndex(index: Int) -> CGRect {

        var textViewRect = CGRect(origin: CGPointZero, size: CGSize(width: CGRectGetWidth(bounds)/2, height: CGRectGetHeight(bounds)))
        textViewRect = CGRectInset(textViewRect, 10, 20)
        textViewRect = CGRectOffset(textViewRect, CGRectGetWidth(bounds) / 2 * CGFloat(index), 0)
        return textViewRect
    }
}

这篇关于如何基于框架的大小分隔UIScrollView/UITextView的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆