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

查看:60
本文介绍了以编程方式确定文本框中的最大适合度(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天全站免登陆