Ios 获取UILabel中第一行的字符串

Ios 获取UILabel中第一行的字符串,ios,objective-c,uilabel,Ios,Objective C,Uilabel,我在ui标签中有一些文本。我想获取字符串变量中第一个标签的文本 例如: label.text = @"You make an object by creating an instance of a particular class. You do this by allocating the object and initializing it with acceptable default values. When you allocate an object, you set aside e

我在
ui标签中有一些文本。我想获取字符串变量中第一个标签的文本

例如:

label.text = @"You make an object by creating an instance of a particular class. You do this by allocating the object and initializing it with acceptable default values. When you allocate an object, you set aside enough memory for the object and set all instance variables to zero. Initialization sets an object’s initial state—that is, its instance variables and properties—to reasonable values and then returns the object. The purpose of initialization is to return a usable object. You need to both allocate and initialize an object to be able to use it.";

现在我想获取
UILabel

1.addcoretext.framework中第一行的文本。2.导入#导入CoreText/CoreText.h>。 然后使用下面的方法-

-(NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label
{
    NSString *text = [label text];
    UIFont   *font = [label font];
    CGRect    rect = [label frame];

    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:( id)myFont range:NSMakeRange(0, attStr.length)];

    CFRelease(myFont);

    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));

    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);

    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);

    NSMutableArray *linesArray = [[NSMutableArray alloc]init];

    for (id line in lines)
    {
        CTLineRef lineRef = ( CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);

        NSString *lineString = [text substringWithRange:range];

        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));

        //NSLog(@"''''''''''''''''''%@",lineString);
        [linesArray addObject:lineString];

    }
    [attStr release];

    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);


    return (NSArray *)linesArray;
}
我从下面的url找到了这个答案-

这可能对你有帮助

var array = string.componentsSeparatedByString("\r")

这将用新行分隔字符串,您可以在数组中获得逐行文本。

您所说的“第一行文本”是什么意思?意思是我在标签上有文本,第一行包含文本“您通过创建特定类的实例来创建对象。您这样做”,然后第二行是从“这是通过分配对象并使用可接受的默认值初始化它来实现的”现在我需要来自“youmake…”的第一行文本。。。。“您愿意。”是否使用换行符“\n”作为新行?
NSString *text = label.text;

//remove any leading or trailing whitespace or line breaks
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

//find the the range of the first occuring line break, if any.
NSRange range = [text rangeOfString:@"\n"];

//if there is a line break, get a substring up to that line break
if(range.location != NSNotFound)
    text = [text substringToIndex:range.location];
NSString *text = label.text;

//remove any leading or trailing whitespace or line breaks
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

//find the the range of the first occuring line break, if any.
NSRange range = [text rangeOfString:@"\n"];

//if there is a line break, get a substring up to that line break
if(range.location != NSNotFound)
    text = [text substringToIndex:range.location];