本文介绍了调整UILabel大小以适合Word Wrap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是iPhone应用程序的一部分,但应该适用于一般用objc编写的Cocoa。 我有一个UILabel持有各种数量的文本几句话)。文本应始终以适合UILabel中所有文本的最大可能字体显示。 最大行数设置为4,换行模式设置为换行。 由于使用了多行,adjustFontSizeToFitWidth将不起作用 因此,我使用循环来确定每个字符串最大可能的字体大小:这种方法大部分工作良好。 异常是长字。 让我们接受string @经验foo。举个例子。 经验这个词比其他词长得多,不会被正确的分词,字符串分成4行。 我正在寻找一种方法来进一步缩小尺寸,以便每个单词适合一行。 示例: -old - 字体大小:60 这可能是一个简单的方法,但我打墙。 p> 解决方案这是我发现使这项工作最优雅(但有点黑客)的方式: 将字符串拆分为单词 使用当前字体大小计算每个单词的宽度 减少字符串的大小,直到每个字符合一行 资源消耗足够低,即使在 这是新的代码: //设置文本 self.textLabel.text = text; //使用的最大大小 NSInteger fsize = 200; textLabel.font = [UIFont fontWithName:@Verdana-Bold size:fsize]; //使用当前参数计算渲染字符串的大小 float height = [text sizeWithFont:textLabel.font constrainedToSize:CGSizeMake(textLabel.bounds。 size.width,99999) lineBreakMode:UILineBreakModeWordWrap] .height; //如果没有高度(空字符串),将字体大小减少5,如果没有高度,则中断; while(height> textLabel.bounds.size.height and height!= 0){ fsize - = 5; textLabel.font = [UIFont fontWithName:@Verdana-Boldsize:fsize]; height = [text sizeWithFont:textLabel.font constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) lineBreakMode:UILineBreakModeWordWrap] .height; }; //循环遍历字符串中的单词并调整大小以适应([text componentsSeparatedByString:@]中的NSString * word){ float width = [word sizeWithFont: textLabel.font] .width; while(width> textLabel.bounds.size.width and width!= 0){ fsize - = 3; textLabel.font = [UIFont fontWithName:@Verdana-Boldsize:fsize]; width = [word sizeWithFont:textLabel.font] .width; } } This is part of an iPhone application but should apply to Cocoa written in objC in general.I have a UILabel holding various amounts of text (from single characters to several sentences). The text should always be displayed in the largest possible font that fits all the text within the UILabel.The maximum number of lines is set to 4 and the line break mode is set to word wrap.Since multiple lines are used, adjustsFontSizeToFitWidth won't work for resizing the text.Thus I am using a loop to determine the largest possible font size for each string as such:This approach works well for the most part.The exception are long words.Let's take the string @"The experience foo." as an example.The word "experience", being much longer than the others will be split in half without being word-wrapped correctly and the string split across 4 lines.I am looking for a way to reduce the size further so that each individual word fits in one line.Example:-old-Font size: 60should be-new-Font size: 30There probably is an easy way to do this but I'm hitting a wall. 解决方案 Here is the most elegant (yet somewhat hackish) way I found to make this work:Split the string into wordsCalculate the width of each word using the current font sizeReduce the size of the string until each the word fits into one lineResource consumption is low enough for this to work even in UITableViews full of strings edited this way.Here is the new code://Set the textself.textLabel.text = text;//Largest size usedNSInteger fsize = 200; textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:fsize];//Calculate size of the rendered string with the current parametersfloat height = [text sizeWithFont:textLabel.font constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) lineBreakMode:UILineBreakModeWordWrap].height;//Reduce font size by 5 while too large, break if no height (empty string)while (height > textLabel.bounds.size.height and height != 0) { fsize -= 5; textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:fsize]; height = [text sizeWithFont:textLabel.font constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) lineBreakMode:UILineBreakModeWordWrap].height;};// Loop through words in string and resize to fitfor (NSString *word in [text componentsSeparatedByString:@" "]) { float width = [word sizeWithFont:textLabel.font].width; while (width > textLabel.bounds.size.width and width != 0) { fsize -= 3; textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:fsize]; width = [word sizeWithFont:textLabel.font].width; }} 这篇关于调整UILabel大小以适合Word Wrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 03:54