2

I have some text in a UILabel. I want to get the text of the first label in a string variable.

For example:

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.";

Now I want to get the text of the first line in the UILabel.

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Yatindra Jain
  • 325
  • 1
  • 4
  • 11
  • what do you mean by "text of first line"? – Clement Prem May 25 '15 at 09:48
  • Means i have text on lable, on which first line contain text "You make an object by creating an instance of a particular class. You do" then second is start from "this by allocating the object and initializing it with acceptable default" now i need first line text from "You make .... " to "You do". – Yatindra Jain May 25 '15 at 10:13
  • Are you using line break '\n' for new line ? – Omer Waqas Khan May 25 '15 at 12:54

3 Answers3

3

1.Add CoreText.framework. 2. Import #import CoreText/CoreText.h>. Then use below method -

-(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;
}

I found this answer from below url - How to get text from nth line of UILabel?

NSString *firstLineString = [[self getLinesArrayOfStringInLabel:yourLabel] objectAtIndex:0];
Community
  • 1
  • 1
Santu C
  • 2,644
  • 2
  • 12
  • 20
3
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];
Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62
0

This may help you

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

This will separate the string with new line, and you can get the line wise text in array.

Shardul
  • 4,266
  • 3
  • 32
  • 50