How to edit Align UILabel on a cell table screen?

I am creating one project that has a tableview class in itself. I have 3 things in any cell of the table. (1 button (left side) and 2 liters, as the bottom image)

enter image description here

I want the header cell to be correct, but I could not do this.

this is my code. where is my error:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"copymove-cell-bg"]]; [_backgroundImageView setContentMode:UIViewContentModeTopRight]; [self setBackgroundView:_backgroundImageView]; [self setSelectionStyle:UITableViewCellSelectionStyleNone]; _iconButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_iconButton setFrame:CGRectMake(0, 10, 47, 50)]; [_iconButton setAdjustsImageWhenHighlighted:NO]; [_iconButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder"] forState:UIControlStateNormal]; [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder-selected"] forState:UIControlStateSelected]; [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder-selected"] forState:UIControlStateHighlighted]; [self.contentView addSubview:_iconButton]; _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, _titleTextField.frame.size.width, _titleTextField.frame.size.height)]; [_titleTextField setFont:KOFONT_FILES_TITLE]; [_titleTextField setTextColor:KOCOLOR_FILES_TITLE]; [_titleTextField.layer setShadowColor:KOCOLOR_FILES_TITLE_SHADOW.CGColor]; [_titleTextField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin]; [_titleTextField.layer setShadowOffset:CGSizeMake(0, 1)]; [_titleTextField.layer setShadowOpacity:1.0f]; [_titleTextField.layer setShadowRadius:0.0f]; [_titleTextField setTextAlignment:UITextAlignmentRight]; [_titleTextField setLineBreakMode:UILineBreakModeMiddleTruncation]; [self.contentView addSubview:_titleTextField]; [self.layer setMasksToBounds:YES]; _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(273, 29, 47, 28)]; [_countLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin]; [_countLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"item-counter"]]]; [_countLabel setTextAlignment:UITextAlignmentCenter]; [_countLabel setLineBreakMode:UILineBreakModeMiddleTruncation]; [_countLabel setFont:KOFONT_FILES_COUNTER]; [_countLabel setTextColor:KOCOLOR_FILES_COUNTER]; [_countLabel setShadowColor:KOCOLOR_FILES_COUNTER_SHADOW]; [_countLabel setShadowOffset:CGSizeMake(0, 1)]; [self setAccessoryView:_countLabel]; [self.accessoryView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin]; } return self; } 
+4
source share
7 answers

If you are using iOS6,

use [_countLabel setTextAlignment:NSTextAlignmentCenter]; because the UITextAlignmentCenter is deprecated in iOS6.

To align to the right,

 [_countLabel setTextAlignment:NSTextAlignmentRight]; 

Hope this helps you ...

+1
source

Your code works fine for my part. Check the width and height that you pass on the labels. Just check your code by setting the width and height of your tags.

Hope this helps you.

0
source
 _titleTextField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight 
0
source

The first UITextAlignmentRight is deprecated in iOS6 , so you should use:

 _titleTextField.textAlignment = 2; 

Secondly, you need to check the UILabel Frame . Check out CGFloat x, CGFloat y and width your UILable .

textAlignment = 0 (LeftAlignment). textAlignment = 1 (CenterAlignment). textAlignment = 2 (RightAlignment).

Hope this helps.

0
source

If aligning the text inside the label is your problem, then the above results are good. If setting the framework is a problem, then I suggest saving the code something like

Ask the nib say the Cell.xib file and the corresponding .m and .h say the Cell.m and Cell.h file that owns the nib file. The Cell class must be a subclass of UITableViewCell . Place the UILables/UIImageView/UIButton in xib at the desired positions and map them to the corresponding properties in the Cell class (also specify < Cell identifier in Cell.xib ).

In cellForRowatIndex datasource method gets cells from nib like -

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Cell *cell = (Cell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//same CellIdentifier as in the Cell.xib if(cell==nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil] lastObject]; } //Access and set the labels, imageViews here return cell; } 
0
source
 _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, _titleTextField.frame.size.width, _titleTextField.frame.size.height)]; 

This is strange. You set the width and height of the label to its width and height. This will set it to the width and height of the previous label. I do not know what it will do the first time you come across this code.

The text is aligned to the right within the label view, but the borders do not end where you want. You can try setting the background color so that you can see where the frame of your label is.

 _titleTextField.backgroundColor = [UIColor yellowColor]; 

You just need to set it to fill the cell space and allow it to place text inside it. Creating a label frame too large does not affect the size of the text, although it makes it too small, depending on the settings that are set.

 _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, 273 - 69, self.contentview.bounds.size.height)] 
0
source

For iOS 10+ and Swift 3+

 titleTextField.textAlignment = .center lblTitle.textAlignment = .right 
0
source

Source: https://habr.com/ru/post/1483090/


All Articles