2009-07-25 33 views
3

我在我的一些項目中使用了以下功能,但最近我創建了一個小例子,暗示它不可靠(至少在某些情況下) :iPhone開發 - 關於resizng UILabel和定製UITableViewCell的另一個問題

[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

+0

補充說: 「結果」 一節。基本上我相信這些方法在計算邊界時不可靠。當然,在文本被截斷並且「...」被附加到字符串以供顯示的情況下,以及其他情況下。 – Mustafa 2009-07-26 07:29:59

0

CGSize大小,constrainedSize;

constrianedSize.width = MAXFLOAT; constrainedSize.height = MAXFLOAT;

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

返回的constrainedSize應該具有正確的文本寬度/高度。

0

我和這個函數有類似的問題,經過很多次的調試後,我終於明白,如​​果你將小於標籤寬度的寬度傳遞給參數constratainedToSize,它可以在所有情況下工作。

請嘗試以下

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

的同時使用UILineBreakModeWordWrap作爲價值UILineBreakMode