开发者

Shrink custom textLabel and detailTextLabel in custom UITableViewCell

I'm using a slightly customized version of Loren Brichter's Fast Scrolling script and I'm having a problem where the labels in the cells don't stop if they reach the end of the cell. When programming table views the standard way, if the textLabels or detailTextLabels are too long, they automatically cut off and apply three dots at the end of the string to stay within the cell.

I want to do exactly the same thing, but I have no idea how to achieve it. Here is开发者_StackOverflow中文版 the code I use when adding text to the table view cell:

CGPoint t;
CGPoint d;

t.x = feedImage.size.width + 10 + 12;
t.y = 20;
[textLabel drawAtPoint:t withFont:textLabelFont];

d.x = feedImage.size.width + 10 + 12;
d.y = 39;
[detailTextLabel drawAtPoint:d withFont:detailTextLabelFont];


Followed this tutorial. Works seamlessly.


You can programmatically find out (using approx. character widths) which would be the last two/three characters visible. Then remove the remaining characters and put the three dots yourself! For upper case characters try a width of 16 and for lower-case, a width of 12 points for a font-size of 15. find out the right numbers by trial and error.

Ok. Here's a function that adjusts the label height(for a given width) according to the characters. The variable "width" is the width of the label and "tempWidth" is the width of current row being calculated. You can modify this function to return the truncated string with the three dots at the end...

#define smallLetterWidth 12
#define capitalLetterWidth 16
-(int) numRowsForString:(NSString *) inputStr width:(int) width{
int j=0;
numRows=1;
int tempWidth = 0;
while(j<[inputStr length]){
    if([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[inputStr characterAtIndex:j]])
        tempWidth += capitalLetterWidth;
    else
        tempWidth += smallLetterWidth;
     if(tempWidth>width){
        tempWidth = 0;
        numRows++;
        j--;
    }
    else if(tempWidth==width)
    {
        tempWidth = 0;
        numRows++;
    }
    j++;
}
return  numRows;
}

Even better: What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜