Objective c 在两个UILabel之间拆分文本字符串以环绕图像
我试图在两个UILabel之间分离一长串文本,以便环绕图像。我已经重新使用和改编了一些以前的开发人员在这个项目上留下的代码,如下所示 字符串(顺序数字字,1到20): 拆分方法Objective c 在两个UILabel之间拆分文本字符串以环绕图像,objective-c,ios,nsarray,uilabel,word-wrap,Objective C,Ios,Nsarray,Uilabel,Word Wrap,我试图在两个UILabel之间分离一长串文本,以便环绕图像。我已经重新使用和改编了一些以前的开发人员在这个项目上留下的代码,如下所示 字符串(顺序数字字,1到20): 拆分方法 -(void)seperateTextIntoLabels:(NSString*) text { // Create array of words from our string NSArray *words = [text componentsSeparatedByCharactersInSet:[NS
-(void)seperateTextIntoLabels:(NSString*) text
{
// Create array of words from our string
NSArray *words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
//Data storage for loop
NSMutableString *text1 = [[NSMutableString alloc] init];
NSMutableString *text2 = [[NSMutableString alloc] init];
for(NSString *word in words)
{
CGSize ss1 = [[NSString stringWithFormat:@"%@ %@",text1,word] sizeWithFont:descriptionLabel.font constrainedToSize:CGSizeMake(descriptionLabel.frame.size.width, 9999) lineBreakMode:descriptionLabel.lineBreakMode];
if(ss1.height > descriptionLabel.frame.size.height || ss1.width > descriptionLabel.frame.size.width)
{
if( [text2 length]>0)
{
[text2 appendString: @" "];
}
[text2 appendString: word];
}
else {
if( [text1 length]>0)
{
[text1 appendString: @" "];
}
[text1 appendString:word];
}
}
descriptionLabel.text = text1;
descriptionLabelTwo.text = text2;
[descriptionLabel sizeToFit];
[descriptionLabelTwo sizeToFit];
}
它的工作原理和我预期的差不多,只是在切换发生的时候它变得混乱了
注意标签1“One”中的最后一个单词放错了位置。这个词在第二个标签的中间部分也丢失了。除了这个问题,它似乎工作正常,否则
你知道这里发生了什么吗
有没有其他解决办法?请注意,我不想使用UIWebView(主要是因为屏幕渲染延迟)。好的,这是您的问题 您正在检查字符串中的每个单词是否符合第一个标签,然后检查是否不符合第一个标签。它进入第二个阶段。直到“十二点”为止,它都符合第一个标签。但是你想让剩下的字符串落入第二个标签,对吗 在你的检查中,即使在你分割到第二个标签之后,你也会继续检查是否每个单词都适合第一个标签。“一”是第一个仍然适合第一个标签的单词,因此将其放置在第一个标签中,并继续将其他单词放置在第二个标签中 要解决这个“奇怪”的拆分问题,您可以将自己设置为一个布尔值,当您将拆分为第二个标签时,该布尔值将变为“是”(或您喜欢的true w/e),并确保您检查该布尔值是否已打开,以及检查大小 我希望这一切现在对你有意义 祝你好运
-(void)seperateTextIntoLabels:(NSString*) text
{
// Create array of words from our string
NSArray *words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
//Data storage for loop
NSMutableString *text1 = [[NSMutableString alloc] init];
NSMutableString *text2 = [[NSMutableString alloc] init];
for(NSString *word in words)
{
CGSize ss1 = [[NSString stringWithFormat:@"%@ %@",text1,word] sizeWithFont:descriptionLabel.font constrainedToSize:CGSizeMake(descriptionLabel.frame.size.width, 9999) lineBreakMode:descriptionLabel.lineBreakMode];
if(ss1.height > descriptionLabel.frame.size.height || ss1.width > descriptionLabel.frame.size.width)
{
if( [text2 length]>0)
{
[text2 appendString: @" "];
}
[text2 appendString: word];
}
else {
if( [text1 length]>0)
{
[text1 appendString: @" "];
}
[text1 appendString:word];
}
}
descriptionLabel.text = text1;
descriptionLabelTwo.text = text2;
[descriptionLabel sizeToFit];
[descriptionLabelTwo sizeToFit];
}