以编程方式确定最大适合文本框 (WP7) [英] Programmatically determining max fit in textbox (WP7)

查看:26
本文介绍了以编程方式确定最大适合文本框 (WP7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写适用于 Windows Phone 7 的电子书阅读器,我正在尝试将其设计成 Kindle 阅读器的样式.为此,我需要将我的书分成几页,当添加可变字体大小时,这会变得更加复杂.

I'm currently writing an eBook reader for Windows Phone Seven, and I'm trying to style it like the Kindle reader. In order to do so, I need to split my books up into pages, and this is going to get a lot more complex when variable font sizes are added.

为了做到这一点,我每次只在文本块中添加一个单词,直到它高于其容器.但是,您可以想象,对于超过 120,000 字的文档,这需要一段无法接受的时间.

To do this at the moment, I just add a word at a time into the textblock until it becomes higher than its container. As you can imagine though, with a document of over 120,000 words, this takes an unacceptable period of time.

有没有一种方法可以找出文本何时超出边界(逻辑上将其划分为页面),而无需实际渲染它?这样我就可以在后台线程中运行它,这样用户就可以在此期间继续阅读.

Is there a way I can find out when the text would exceed the bounds (logically dividing it into pages), without having to actually render it? That way I'd be able to run it in a background thread so the user can keep reading in the meantime.

到目前为止,我想到的唯一想法是找出文本块如何决定其边界(在测量调用中?),但我不知道如何找到该代码,因为反射器没有显示任何内容.

So far, the only idea that has occurred to me is to find out how the textblock decides its bounds (in the measure call?), but I have no idea how to find that code, because reflector didn't show anything.

提前致谢!

推荐答案

我做了一些类似的事情来调整单个文本框的字体大小(以确保它们都适合).基本上,我在代码中创建一个 TextBlock,设置我的所有属性并检查 ActualWidth 和 ActualHeight 属性.这里有一些伪代码可以帮助您解决问题:

I do something similar to adjust font size for individual textboxes (to ensure they all fit). Basically, I create a TextBlock in code, set all my properties and check the ActualWidth and ActualHeight properties. Here is some pseudo code to help with your problem:

public static String PageText(TextBlock txtPage, String BookText)
{
    TextBlock t = new TextBlock();
    t.FontFamily = txtPage.FontFamily;
    t.FontStyle = txtPage.FontStyle;
    t.FontWeight = txtPage.FontWeight;
    t.FontSize = txtPage.FontSize;
    t.Text = BookText;

    Size Actual = new Size();
    Actual.Width = t.ActualWidth;
    Actual.Height = t.ActualHeight;

    if(Actual.Height <= txtPage.ActualHeight)
        return BookText;

    Double hRatio = txtPage.ActualHeight / Actual.Height;
    return s.Substring((int)((s.Length - 1) * hRatio));
}

以上是未经测试的代码,但希望可以帮助您入门.基本上它会查看文本是否可以放入框中,如果可以,您就可以开始了.如果不是,它会找出适合的文本百分比并返回它.这不考虑分词,可能不是完美匹配,但应该会让您接近.

The above is untested code, but hopefully can get you started. Basically it sees if the text can fit in the box, if so you're good to go. If not, it finds out what percentage of the text can fit and returns it. This does not take word breaks into account, and may not be a perfect match, but should get you close.

您可以更改此代码以返回长度而不是实际的子字符串,并将其用作页面大小.在代码中创建文本块(不显示)实际上执行得很好(我在一些没有明显延迟的表视图中这样做).我不会将所有 120,000 个单词发送给这个函数,而是发送某种合理的子集.

You could alter this code to return the length rather than the actual substring and use that as your page size. Creating the textblock in code (with no display) actually performs pretty well (I do it in some table views with no noticeable lag). I wouldn't send all 120,000 words to this function, but a reasonable subset of some sort.

一旦您有了理想的长度,您就可以使用 RegEx 将书分成几页.RegEx 的这个站点上有一些示例,在特定长度后会打破单词边界.

Once you have the ideal length you can use a RegEx to split the book into pages. There are examples on this site of RegEx that break on word boundaries after a specific length.

另一种选择是提前计算每个潜在字体大小的页面大小(并使用 switch 语句对其进行硬编码).如果你允许任何字体和任何大小的组合,这很容易变得疯狂,如果你允许混合字体/大小,这会很糟糕,但会表现得很好.很可能您有特定范围的可读大小,并且只有几种字体.创建一个测试应用程序来计算每个组合的页面文本长度不会那么难,并且可能会让您的生活更轻松 - 即使它感觉"不适合作为程序员:)

Another option, is to calculate page size ahead of time for each potential fontsize (and hardcode it with a switch statement). This could easily get crazy if you are allowing any font and any size combinations, and would be awful if you allowed mixed fonts/sizes, but would perform very well. Most likely you have a particular range of readable sizes, and just a few fonts. Creating a test app to calculate the text length of a page for each of these combinations wouldn't be that hard and would probably make your life easier - even if it doesn't "feel" right as a programmer :)

这篇关于以编程方式确定最大适合文本框 (WP7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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