我有一个参数numberOfLines设置为2的UILabel。我想从这个中创建2个UIlabel,第一个是第一行,第二个是第二行。有没有办法实现呢?
答案 0 :(得分: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];
}