Adam
Adam

Reputation: 9049

Calculating enough text to fit within existing UILabel

I can't get some CoreText text wrapping code working for me; it's just too complicated. I'm going to try and go another route, which is to split my UILabel into two.

What I'm trying to achieve is to have my text appear to wrap around my fixed sized rectangular image. It'll always be the same dimensions.

So, when the UILabel next to the image fills up exactly, it'll create another UILabel below the image.

Now, how do I calculate the text in the first UILabel and have it fit nicely in the entire width of the UILabel, without being too short or cut off at the end?

Upvotes: 1

Views: 694

Answers (2)

Ashwin
Ashwin

Reputation: 2317

Well, this ought to work to get the substring of the master string that will fit within the desired width:

//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...

Of course, this is assuming you want the separation to occur using word wrapping. If you don't mind words themselves being cut apart (i.e. character wrap) in order to fully take up the desired width, do this instead:

NSString *tempstring = masterString;

while (someLabel.bounds.size.width < [tempString sizeWithFont:someLabelLabel.font].width) {
    tempString = [tempString substringToIndex:tempString.length - 1];
}
//Now do whatever you want with the tempString, which will fit in the width desired...

In order to get the remaining piece of the string left over, do this:

NSString *restOfString = [masterString substringFromIndex:tempString.length];

Hope this helps. I have to admit that I haven't properly tested this code yet, though I've done something similar in the past...

Upvotes: 1

Nikunj Jadav
Nikunj Jadav

Reputation: 3402

Try below link its will help you.

If you want to create a "link" on some custom text in your label, instead of using a WebView as @Fabian Kreiser suggested, you sould use my OHAttributedLabel class (you can find it this link)

See the sample code provided on my github repository: you can use my addCustomLink:inRange: method to add a link (with a customized URL) to a range of text (range that you could determine by iterating over every occurrences of the word "iPhone" in your text very easily). Then in the delegate method on OHAttributedLabel, you can catch when the link is tapped and act accordingly to do whatever you need.

Upvotes: 0

Related Questions