iPhone 开发 - 关于调整 UILabel 和自定义 UITableViewCell 的另一个问题

Posted

技术标签:

【中文标题】iPhone 开发 - 关于调整 UILabel 和自定义 UITableViewCell 的另一个问题【英文标题】:iPhone Development - Another question on resizng UILabel & custom UITableViewCell 【发布时间】:2009-07-25 10:32:55 【问题描述】:

我在一些项目中使用了以下功能,但最近我创建了一个小示例,表明它不可靠(至少在某些情况下):

[NSString sizeWithFont:font constrainedToSize:size]
[NSString sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreak]

我创建了一个自定义 UITableViewCell 类来实现在 iPhone 的相册应用程序中看到的结果,例如图片,带有相册名称和相册中的图片数量。我使用上述功能来调整我的详细文本(用于计数)。代码如下:

Interface File:

#import <UIKit/UIKit.h>

@interface CustomValue2Cell : UITableViewCell 
    UIImageView *cellImage;
    UILabel *cellTextLabel;
    UILabel *cellDetailTextLabel;


@property (nonatomic, retain) UIImageView *cellImage;
@property (nonatomic, retain) UILabel *cellTextLabel;
@property (nonatomic, retain) UILabel *cellDetailTextLabel;

- (void)setCellImage:(UIImage *)image withText:(NSString *)text andDetail:(NSString *)detail;
- (CGSize)getSizeOfText:(NSString *)text withFont:(UIFont *)font;

@end

Implementation File:

#import "CustomValue2Cell.h"

@implementation CustomValue2Cell

@synthesize cellImage;
@synthesize cellTextLabel;
@synthesize cellDetailTextLabel;

// Coordinate positions of Image, Text Label, and Detail Text Label
# define kImageXPosition    3
# define kImageYPosition    3
# define kImageWidth        37.0
# define kImageHeight       37.0

# define kTextXPosition     55
# define kTextYPosition     10
# define kTextWidth         200
# define kTextHeight        22

# define kDetailXPosition   55
# define kDetailYPosition   10
# define kDetailWidth       40
# define kDetailHeight      22

// Displacement value for Text Label with Detail Text Label
// Displacement value of 10-15 works with non-bold font
# define kCellSubviewDisplacement   20  

// Label Font Size for Text Label and Detail Text Label
# define kTextLabelFontSize     18
# define kDetailLabelFontSize   16

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier 
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) 

        // Color and font for the main label
        UIColor *textLabelColor = [UIColor blackColor];
        UIFont *textLabelFont = [UIFont systemFontOfSize:kTextLabelFontSize];

        // Color and font for the detail label
        UIColor *detailTextColor = [UIColor darkGrayColor];
        UIFont *detailTextFont = [UIFont systemFontOfSize:kDetailLabelFontSize];

        // Highligted row color
        UIColor *highlightedTextColor = [UIColor whiteColor];

        // Define boundary of the cell contents
        CGRect rect;

        // Set properties of Image Thumbnail
        rect = CGRectMake(kImageXPosition, kImageYPosition, kImageWidth, kImageHeight);
        cellImage = [[UIImageView alloc] initWithFrame:rect];

        // Set properties of Name
        rect = CGRectMake(kTextXPosition, kTextYPosition, kTextWidth, kTextHeight);
        cellTextLabel = [[UILabel alloc] initWithFrame:rect];
        [cellTextLabel setFont:textLabelFont];
        [cellTextLabel setTextColor:textLabelColor];
        [cellTextLabel setHighlightedTextColor:highlightedTextColor];

        // Set properties of Value
        rect = CGRectMake(kDetailXPosition, kDetailYPosition, kDetailWidth, kDetailHeight);
        cellDetailTextLabel = [[UILabel alloc] initWithFrame:rect];
        [cellDetailTextLabel setFont:detailTextFont];
        [cellDetailTextLabel setTextColor:detailTextColor];
        [cellDetailTextLabel setHighlightedTextColor:highlightedTextColor];

        // Put Image, Name, and Value in Content View
        [[self contentView] addSubview:cellImage];
        [[self contentView] addSubview:cellTextLabel];
        [[self contentView] addSubview:cellDetailTextLabel];

        //Set cell selection style (Blue)
        self.selectionStyle = UITableViewCellSelectionStyleBlue;
    
    return self;


- (void)setCellImage:(UIImage *)image withText:(NSString *)text andDetail:(NSString *)detail 
    cellImage.image = image;
    cellTextLabel.text = text;
    cellDetailTextLabel.text = detail;

    // Get an estimated size of text in the label, 
    // which will be used to estimate the position of detail label
    UIFont *textLabelFont = [UIFont systemFontOfSize:kTextLabelFontSize];
    CGSize size = [self getSizeOfText:text withFont:textLabelFont];

    // Re-set the frame of detail view
    CGRect frame = CGRectMake(kDetailXPosition, kDetailYPosition, kDetailWidth, kDetailHeight);
    frame.origin.x = frame.origin.x + size.width + kCellSubviewDisplacement;
    [cellDetailTextLabel setFrame:frame];


- (CGSize)getSizeOfText:(NSString *)text withFont:(UIFont *)font 
    return [text sizeWithFont:font constrainedToSize:CGSizeMake(kTextWidth, kTextHeight)];


- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state


- (void)dealloc 
    [cellImage release];
    [cellTextLabel release];
    [cellDetailTextLabel release];
    [super dealloc];


@end

为了测试我的自定义类,我准备了一个字体列表并将它们显示在我的表格视图中。获取可用字体列表的代码:

fontFamily = [[NSMutableArray alloc] initWithArray:[UIFont familyNames]];
fonts = [[NSMutableArray alloc] init];

for(NSUInteger i=0; i<[fontFamily count]; i++) 
    NSString *familyName = [fontFamily objectAtIndex:i];
    NSArray *array = [UIFont fontNamesForFamilyName:familyName];
    [fonts addObjectsFromArray:array];

这是返回单元格的代码:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    static NSString *CellIdentifier = @"Cell";
    CGRect rect = CGRectMake(0.0, 0.0, 320.0, 50.0);

    CustomValue2Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[CustomValue2Cell alloc] initWithFrame:rect reuseIdentifier:CellIdentifier] autorelease];
    

    NSString *font = [fonts objectAtIndex:indexPath.row];
    NSString *detail = [NSString stringWithFormat:@"%d", [[fonts objectAtIndex:indexPath.row] length]];

    [cell setCellImage:[UIImage imageNamed:@"Pointy.gif"] withText:font andDetail:detail];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]
    return cell;

Result: Text Label and Detail Text Label are overlapping for some values. e.g. "CouierNewPS-BoldMT" and "25" overlap each other, and in some other cases, they are not overlapping but they are very close.

Question: Is their a better alternative to -sizeWithFont function? Or, am i doing something very stupid to mess things up?

抱歉,发了很长的帖子。任何帮助表示赞赏。期待中的感谢。

【问题讨论】:

【参考方案1】:

好吧,你从来没有真正确切地说出问题出在哪里。它返回的尺寸是太大、太小、全部在一条线上,还是完全不同?

当我进行字符串大小调整时,我使用

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

并为换行模式指定UILineBreakModeWordWrap。我不确定这是否最终返回基本相同的值,但我发现它对我有用。

参考:NSString UIKit Additions Reference Class Reference

【讨论】:

添加了“结果”部分。基本上我相信这些方法在计算界限时是不可靠的。当然,在文本被截断并将“...”附加到字符串以进行显示的情况下,可能还有其他情况。【参考方案2】:

CGSize 大小,受约束的大小;

constrianedSize.width = MAXFLOAT; 受约束的大小.height = MAXFLOAT;

size = [text sizewithFont:systemFontofSize:14 constrainedToSize:constrinedSize lineBreakMode];

返回的 constrainedSize 应该具有正确的文本宽度/高度。

【讨论】:

【参考方案3】:

这个函数我也遇到过类似的问题,调试了很多次后,我终于想通如果你把小于标签宽度的宽度传递给参数constratainedToSize,它在所有情况下都有效。

请试试下面的

- (CGSize)getSizeOfText:(NSString *)text withFont:(UIFont *)font 
    return [text sizeWithFont:font constrainedToSize:CGSizeMake(kTextWidth - 10, kTextHeight)];

也使用 UILineBreakModeWordWrap 作为 UILineBreakMode 的值

【讨论】:

以上是关于iPhone 开发 - 关于调整 UILabel 和自定义 UITableViewCell 的另一个问题的主要内容,如果未能解决你的问题,请参考以下文章

iPhone - 根据文字调整 UILabel 宽度

字体更改时自动uiLabel和uiTableViewCell调整大小的iphone代码?

将UIView中UILabel的字体大小调整为不同的设备大小

iPad上的UILabel文本未调整大小

IOS开发调整UILabel的行间距

调整UILabel的大小以适应Word Wrap