2010-08-12 17 views
58

Jestem trochę nowy w rozwoju Objective-C i iPhone'a i natknąłem się na problem podczas próby wyśrodkowania tekstu w komórce tabeli. Szukałem google, ale rozwiązania są dla starego błędu SDK, który został naprawiony, a te nie działają dla mnie.Wyśrodkuj Wyrównaj tekst w problemie UITableViewCell

Niektóre kodu:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = @"Please center me"; 
    cell.textLabel.textAlignment = UITextAlignmentCenter; 
    return cell; 
} 

Powyższe nie wyśrodkować tekst.

Próbowałem również metodę willDisplayCell:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.textLabel.textAlignment = UITextAlignmentCenter; 
} 

a próbowałem kilka starych zamieszczonych rozwiązań:

UILabel* label = [[[cell contentView] subviews] objectAtIndex:0]; 
label.textAlignment = UITextAlignmentCenter; 
return cell; 

Żaden z nich nie ma żadnego wpływu na wyrównanie tekstu. Nie mam pojęcia, że ​​jakakolwiek pomoc byłaby najbardziej ceniona.

Wiwaty z góry.

+11

UITextAlignment jest przestarzałe w iOS 6.0, to teraz NSTextAlignment – Souljacker

Odpowiedz

120

nie wiem, czy to pomaga konkretnego problemu, jednak UITextAlignmentCenter działa jeśli używasz initWithStyle:UITableViewCellStyleDefault

+4

To pomogło, bo nie używałem subtelne dla komórek chciałem środka. Dzięki. Chyba UITextAlignmentCenter nie działa dla komórek UITableViewCellStyleSubtitle. – Magpie

+2

'UITextAlignmentCenter' jest teraz przestarzała. Zobacz [ten post] (http://stackoverflow.com/a/12793054/2521004) za dobrą alternatywę. –

+2

PERFECT. Ktoś z Apple powinien zostać zwolniony, dostarczając nam komunikaty o błędach, które nie wyjaśniają, jak rozwiązywać problemy. Gdyby to nie był stackoverflow i ludzie tacy jak ty, wszyscy bylibyśmy spieprzeni. – SpaceDog

4

Ta przeróbka będzie wyśrodkować tekst przy użyciu UITableViewCellStyleSubtitle. Załaduj obie etykiety tekstowe za pomocą ciągów, a następnie zrób to przed zwróceniem komórki. To może być prostsze po prostu dodać własne UILabels do każdej komórki, ale byłem zdeterminowany, by znaleźć inną drogę ...

// UITableViewCellStyleSubtitle measured font sizes: 18 bold, 14 normal 

UIFont *font = [UIFont boldSystemFontOfSize:18]; // measured after the cell is rendered 
CGSize size = [cell.textLabel.text sizeWithFont:font]; 
CGSize spaceSize = [@" " sizeWithFont:font]; 
float excess_width = (cell.frame.size.width - 16) - size.width; 
if (cell.textLabel.text && spaceSize.width > 0 && excess_width > 0) { // sanity 
    int spaces_needed = (excess_width/2.0)/spaceSize.width; 
    NSString *pad = [@"" stringByPaddingToLength:spaces_needed withString:@" " startingAtIndex:0]; 
    cell.textLabel.text = [pad stringByAppendingString:cell.textLabel.text]; // center the text 
} 

font = [UIFont systemFontOfSize:14]; // detail, measured 
size = [cell.detailTextLabel.text sizeWithFont:font]; 
spaceSize = [@" " sizeWithFont:font]; 
excess_width = (cell.frame.size.width - 16) - size.width; 
if (cell.detailTextLabel.text && spaceSize.width > 0 && excess_width > 0) { // sanity 
    int spaces_needed = (excess_width/2.0)/spaceSize.width; 
    NSString *pad = [@"" stringByPaddingToLength:spaces_needed withString:@" " startingAtIndex:0]; 
    cell.detailTextLabel.text = [pad stringByAppendingString:cell.detailTextLabel.text]; // center the text 
} 
+1

nie działał dla mnie – marciokoko

15

To nie działa, ponieważ textLabel jest tak szeroki, jak to musi być dla dowolny określony tekst. (UITableViewCell przesuwa etykiety dookoła zgodnie z ustawieniami w stylu). Można przesłonić widok układu, aby upewnić się, że etykiety zawsze wypełniają całą szerokość komórki.

- (void) layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height); 
    self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height); 
} 

Pamiętaj, aby zachować wysokość/y pozycję samo, bo tak długo jak tekście detailTextLabel „s jest pusty textLabel zostanie wyśrodkowany w pionie.

+0

Te linie w połączeniu z: 'self.detailTextLabel.textAlignment = UITextAlignmentRight;' i 'self.textLabel.textAlignment = UITextAlignmentRight;' Zrobiłem _exactly_ to, czego szukałem i pozwolił mi dalej używać UITableViewCellStyleSubtitle. –

0

W CustomTableViewCell.m:

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.contentView.frame.size.width, self.textLabel.frame.size.height); 

} 

W tabeli metoda:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
    cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = @"Title"; 
    cell.textLabel.textAlignment = UITextAlignmentCenter; 

    return cell; 
} 

Jeśli to konieczne, to samo można powtórzyć dla self.detailTextLabel

0

W takiej samej sytuacji utworzonego niestandardowego UITableViewCell z niestandardowa etykieta:

MCCenterText Plik Cell.h:

#import <UIKit/UIKit.h> 

@interface MCCenterTextCell : UITableViewCell 

@property (nonatomic, strong) UILabel *mainLabel; 

@end 

MCCenterTextCell.m file:

#import "MCCenterTextCell.h" 


@interface MCCenterTextCell() 


@end 


@implementation MCCenterTextCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     self.accessoryType = UITableViewCellAccessoryNone; 
     self.selectionStyle = UITableViewCellSelectionStyleGray; 
     _mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 30)]; 
     _mainLabel.font = BOLD_FONT(13); 
     _mainLabel.textAlignment = NSTextAlignmentCenter; 
     [self.contentView addSubview:_mainLabel]; 

    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 


@end 
3

użyć tego kodu:

cell.textLabel.textAlignment = NSTextAlignmentCenter; 

powyższy kod będzie działać. nie używaj UITextAlignmentCenter, jest przestarzała.

+0

Rzeczywiście to działa - dzięki! –

0

Można użyć kodu, aby wyśrodkować tekst

cell.indentationLevel = 1;

cell.indentationWidth = [UIScreen mainScreen] .bounds.size.width/2-10;

0

W przypadku jedno życzenie, aby wyrównać tekst do prawej, miałem sukcesy adaptując rozwiązanie opisane here.

cell.transform = CGAffineTransformMakeScale(-1.0, 1.0); 
cell.textLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0); 
cell.detailTextLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);