计算每 N 个字符换行一次?
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你自己不要这样做。让
UILabel
或UITextView
或UIWebView
为您完成此操作。每个字母都有不同的宽度。为每N
个字符插入换行符没有任何意义。我的意思是,排版是一门复杂的艺术,自古腾堡时代以来就已经培育了数百年。我自己也不敢这么做。将其留给专家(即 API 实施者)。
Don't do that yourself. Let the
UILabel
orUITextView
orUIWebView
do that for you. Each letters have different width. It doesn't make any sense to insert line breaks for everyN
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.)
不确定您提供的这个复杂的解决方案是否是一个笑话:-)
不需要任何格式化程序等。
编辑:无论如何,您可能会对固定行宽感到失望,因为自动换行无论如何都会(应该!)使您的行更短。
Not sure if this complicated solution you gave was a joke :-)
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.
我不确定你到底想在这里完成什么,但你可能无法使用 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;