Reputation: 423
I have a UITextView that is displaying a facebook status loaded from Facebook Connect. I'm trying to make it so that the UITextView is just creating a preview of the text. I want it to look like it does when there is too much text for a UILabel. It would be something like "There is too much text..." with the dots but UITextViews don't do that. Does anybody know how to get it to work?
Upvotes: 1
Views: 2509
Reputation: 8444
Try the below code. It will display two dots to the textview with text more than its frame height.
if(textview.contentSize.height > textview.frame.size.height)
{
while (textview.contentSize.height > textview.frame.size.height)
{
textview.text = [textview.text substringWithRange:NSMakeRange(0, textview.text.length-1)];
}
textview.text = [textview.text substringWithRange:NSMakeRange(0, textview.text.length-2)];
textview.text= [NSString stringWithFormat:@"%@..",textview.text];
}
It works, only when we set the correct height to the UITextview
with respect to the font and fontsize of that textview.
For ex, if the font is bold system font of size 16 means, the textview height should be of minimum 30.
Upvotes: 0
Reputation: 834
+(NSString *)getTruncatedTextForString:(NSString *)inputString withFont:(UIFont *)font withLength:(int)textViewlength
{
CGSize dotSize=[@"..." sizeWithFont:font];
float dotWidth=dotSize.width;
NSString *outputString=@"";
int reqLength=textViewlength-dotWidth;
for(int i=0;i<inputString.length;i++)
{
NSString *tempStr=[outputString stringByAppendingString:[inputString substringWithRange:NSMakeRange(i,1)]];
if([tempStr sizeWithFont:font].width>reqLength)
{
break;
}
else
{
outputString=tempStr;
}
}
NSString *tempStr=[outputString stringByAppendingString:@"..."];
outputString=tempStr;
return outputString;
}
Upvotes: 0
Reputation: 1684
Write a separate method that counts how many letters there are in the string and if there are more than some preset value then cut it and append three dots to the end.
Also, consider using UILabels instead of UITextViews if you don't need to edit information inside since UITextViews take longer to allocate and init and are generally slower than UILabels.
Upvotes: 3