计算每 N 个字符换行一次?

发布于 2024-09-19 17:56:15 字数 910 浏览 10 评论 0原文

我正在为 iPhone 和 iPod Touch 制作一个 RSS 解析器,我需要能够设置每篇文章的每个单元格中显示的行数。我想每隔 39 个字符进行拆分,这是我所使用的字体大小下最适合一行的字符。

我想获取字符串中的字符数并将其除以每行的字符数。我想要一个整数作为答案,并在必要时四舍五入。

这是我到目前为止所拥有的。怎么了?

NSNumber *lines = [[NSNumber alloc] initWithInteger:[cell.textLabel.text length]/kLineLength];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

    [formatter setRoundingMode:NSNumberFormatterRoundUp];
    [formatter setRoundingIncrement:[[[NSNumber alloc]initWithInteger:1]autorelease]];

    NSNumber *roundedLines = [[NSNumber alloc]initWithInteger: [[formatter numberFromString:[lines stringValue]]integerValue]];

    [cell.textLabel setNumberOfLines:[roundedLines integerValue]];

    [roundedLines release];
    [formatter release];
    [lines release];

编辑

我不关心换行符,而是知道要给标签多少行。因此,虽然我有效地设置了换行符,但我并没有真正这样做。我让 iOS 弄清楚在哪里中断,我只是告诉它要使用多少行。

I'm making an RSS parser for iPhone and iPod Touch and I need to be able to set the number of lines shown in each cell for each article. I'd like to break every 39 characters, which is the most that fits on a line at the font size that I'm using.

I'd like to take the number of characters in a string and divide it by the number of characters per line. I'd like to have an integer as an answer, rounded up where necessary.

This is what I have so far. What is wrong?

NSNumber *lines = [[NSNumber alloc] initWithInteger:[cell.textLabel.text length]/kLineLength];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

    [formatter setRoundingMode:NSNumberFormatterRoundUp];
    [formatter setRoundingIncrement:[[[NSNumber alloc]initWithInteger:1]autorelease]];

    NSNumber *roundedLines = [[NSNumber alloc]initWithInteger: [[formatter numberFromString:[lines stringValue]]integerValue]];

    [cell.textLabel setNumberOfLines:[roundedLines integerValue]];

    [roundedLines release];
    [formatter release];
    [lines release];

EDIT

I'm not after line breaks so much as knowing how many lines to give the label. So, while I'm effectively setting line-breaks, I'm not really doing that. I let the iOS figure out where to break, I just tell it how many lines to use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方圜几里 2024-09-26 17:56:15

你自己不要这样做。让 UILabelUITextViewUIWebView 为您完成此操作。每个字母都有不同的宽度。为每 N 个字符插入换行符没有任何意义。

我的意思是,排版是一门复杂的艺术,自古腾堡时代以来就已经培育了数百年。我自己也不敢这么做。将其留给专家(即 API 实施者)。

Don't do that yourself. Let the UILabel or UITextView or UIWebView do that for you. Each letters have different width. It doesn't make any sense to insert line breaks for every N characters.

I mean, typesetting is a sophisticated art, cultivated since the days of Gutenberg for hundreds of years. I don't dare to do that myself. Leave that to the experts (i.e. API implementers.)

迟到的我 2024-09-26 17:56:15

不确定您提供的这个复杂的解决方案是否是一个笑话:-)

int numberOfCharacters = ....;
int kLineLength = 39;

int lines = (numberOfCharacters + kLineLength - 1) / kLineLength;

不需要任何格式化程序等。

编辑:无论如何,您可能会对固定行宽感到失望,因为自动换行无论如何都会(应该!)使您的行更短。

Not sure if this complicated solution you gave was a joke :-)

int numberOfCharacters = ....;
int kLineLength = 39;

int lines = (numberOfCharacters + kLineLength - 1) / kLineLength;

No need for any formatters etc.

Edit: You will probably disappointed with fixed line widths anyway, as word wrapping will (should!) make your lines shorter anyway.

心奴独伤 2024-09-26 17:56:15

我不确定你到底想在这里完成什么,但你可能无法使用 cell.textLabel 并且必须使用自定义 UITableViewCell 。

此外,如果你设置 cell.textLabel,UILabel 对象也会在必要时自动生成换行符。行数 = 2;

Im not sure what you are exactly trying to accomplish here but you may not be able to use cell.textLabel and have to use a custom UITableViewCell..

Also the UILabel object automatically produces the line breaks where necessary if you set the cell.textLabel.numberOfLines = 2;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文