Iphone 计算足够的文本以适应现有UILabel

Iphone 计算足够的文本以适应现有UILabel,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,我不能让一些CoreText文本包装代码为我工作;这太复杂了。我将尝试另一条路线,那就是将我的UILabel一分为二 我试图实现的是让文本环绕固定大小的矩形图像。它将始终是相同的维度 因此,当图像旁边的UILabel完全填满时,它将在图像下方创建另一个UILabel 现在,我如何计算第一个UILabel中的文本,并使其与UILabel的整个宽度很好地匹配,而不会太短或在末尾被截断?尝试下面的链接,它将帮助您 如果您想在标签中的某些自定义文本上创建“链接”,而不是像@Fabian Kreiser建

我不能让一些CoreText文本包装代码为我工作;这太复杂了。我将尝试另一条路线,那就是将我的UILabel一分为二

我试图实现的是让文本环绕固定大小的矩形图像。它将始终是相同的维度

因此,当图像旁边的UILabel完全填满时,它将在图像下方创建另一个UILabel


现在,我如何计算第一个UILabel中的文本,并使其与UILabel的整个宽度很好地匹配,而不会太短或在末尾被截断?

尝试下面的链接,它将帮助您

如果您想在标签中的某些自定义文本上创建“链接”,而不是像@Fabian Kreiser建议的那样使用WebView,那么您应该使用my OHAttributedLabel类


请参阅my github repository上提供的示例代码:您可以使用我的
addCustomLink:inRange:
方法将链接(带有自定义URL)添加到一个文本范围(您可以通过在文本中每次出现“iPhone”一词来轻松确定该范围)。然后,在
OHAttributedLabel
上的委托方法中,您可以捕获点击链接的时间,并根据需要采取相应的行动。

尝试下面的链接,它将帮助您

如果您想在标签中的某些自定义文本上创建“链接”,而不是像@Fabian Kreiser建议的那样使用WebView,那么您应该使用my OHAttributedLabel类


请参阅my github repository上提供的示例代码:您可以使用我的
addCustomLink:inRange:
方法将链接(带有自定义URL)添加到一个文本范围(您可以通过在文本中每次出现“iPhone”一词来轻松确定该范围)。然后,在
OHAttributedLabel
上的委托方法中,您可以捕获链接被点击的时间,并相应地执行所需操作。

好的,这应该可以获得主字符串的子字符串,该字符串将适合所需的宽度:

//masterString is your long string that you're looking to break apart...
NSString *tempstring = masterString;

while (someLabel.bounds.size.width < [tempString sizeWithFont:someLabelLabel.font].width) {
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[tempString componentsSeparatedByString:@" "]];
    //Remove the last object, which is the last word in the string...
    [tempArray removeLastObject];

    //Recreate the tempString with the last word removed by piecing the objects/words back together...
    tempString = @"";
    for (int i=0; i < tempArray.count - 1; i++) {
        tempString = [tempString stringByAppendingFormat:@"%@ ", [tempArray objectAtIndex:i]];
    }
   //You must append the last object in tempArray without the space, or you will get an infinite loop...
    tempString = [tempString stringByAppendingFormat:@"%@", [tempArray objectAtIndex:tempArray.count - 1]];
}
//Now do whatever you want with the tempString, which will fit in the width desired...

希望这有帮助。我必须承认,我还没有正确地测试过这段代码,尽管我在过去做过类似的事情…

好吧,这应该可以让主字符串的子字符串适合所需的宽度:

//masterString is your long string that you're looking to break apart...
NSString *tempstring = masterString;

while (someLabel.bounds.size.width < [tempString sizeWithFont:someLabelLabel.font].width) {
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[tempString componentsSeparatedByString:@" "]];
    //Remove the last object, which is the last word in the string...
    [tempArray removeLastObject];

    //Recreate the tempString with the last word removed by piecing the objects/words back together...
    tempString = @"";
    for (int i=0; i < tempArray.count - 1; i++) {
        tempString = [tempString stringByAppendingFormat:@"%@ ", [tempArray objectAtIndex:i]];
    }
   //You must append the last object in tempArray without the space, or you will get an infinite loop...
    tempString = [tempString stringByAppendingFormat:@"%@", [tempArray objectAtIndex:tempArray.count - 1]];
}
//Now do whatever you want with the tempString, which will fit in the width desired...

希望这有帮助。我必须承认,我还没有正确地测试这段代码,尽管我在过去做过类似的事情…

我想在UIImageView周围环绕文本我想在UIImageView周围环绕文本这如何允许我在图像周围环绕文本?^^sry,我错过了一些。。。(将删除我的评论)这怎么能让我将文字环绕在图像上?^^^^sry,我错过了某个。。。(将删除我的评论)明白一件事。您希望子字符串与“UILabel的整个宽度相匹配,而不会太短或在末尾被截断”,但要使其精确,只有两种方法可以调整字体,使其适合每个标签(这意味着矩形周围的每个子字符串/标签可能具有不同大小的字体)或者你必须使用字符换行(我为其添加了代码),这将保持字体大小不变,但可能会在每行末尾将单词分开…明白一件事。您希望子字符串与“UILabel的整个宽度相匹配,而不会太短或在末尾被截断”,但要使其精确,只有两种方法可以调整字体,使其适合每个标签(这意味着矩形周围的每个子字符串/标签可能具有不同大小的字体)或者您必须使用字符换行(我为其添加了代码),这将保持字体大小不变,但可能会在每行末尾将单词分开。。。
NSString *restOfString = [masterString substringFromIndex:tempString.length];