0

我有一个 UILabel,参数 numberOfLines 设置为 2。我想从这个 UIlabels 制作 2 个 UIlabels,第一个是第一行,第二个是第二行。有什么办法可以做到吗?

4

1 回答 1

2

在这个解决方案中,我假设您UILabel至少有 2 行,并且\n在第一行的末尾有一个字符。

UILabel *lbl = (UILabel *)[self.view viewWithTag:2];

NSString *firstString;
NSString *secondString;

NSRange foundRange = [lbl.text rangeOfString:@"\n"];
if (foundRange.location != NSNotFound)
{
    foundRange.length = foundRange.location + 1;
    foundRange.location = 0;
    firstString = [[NSString alloc] initWithString:[lbl.text substringWithRange:foundRange]];
    secondString = [[NSString alloc] initWithString:[lbl.text stringByReplacingCharactersInRange:foundRange withString:@""]];

    NSLog(@"first line = %@", firstString);
    NSLog(@"second line = %@", secondString);
    [firstString release];
    [secondString release];
}
于 2012-07-24T19:55:38.030 回答