如何仅在合适的单元格中为标签从左到右添加填充
How to add padding from left right only for label in uitable cell
如何只为 UITableViewCell
中的标签从左到右添加填充?
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cellmessage";
SendMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[SendMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setAccessoryType:UITableViewCellAccessoryNone];
NSDictionary *dict = [self->serverArray objectAtIndex: indexPath.row];
cell.celllab.text= [dict objectForKey:@"message"];
CGFloat fixedWidth = cell.celllab.frame.size.width;
CGSize newSize = [cell.celllab sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = cell.celllab.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height + 10);
cell.celllab.frame = newFrame;
cell.celllab.layer.cornerRadius = 4.0f;
cell.celllab.layer.masksToBounds = YES;
//cell.celllab.layer.borderColor = [UIColor blackColor].CGColor;
//cell.celllab.layer.borderWidth = 10.0;
CAShapeLayer *shape = [CAShapeLayer layer];
shape.frame = cell.celllab.bounds;
//shape.path = maskPath.CGPath;
shape.lineWidth = 3.0f;
shape.strokeColor = [UIColor whiteColor].CGColor;
[cell.celllab.layer addSublayer:shape];
if([[dict objectForKey:@"type"] isEqualToString:@"u2a"])
{
cell.celllab.textAlignment = NSTextAlignmentRight;
cell.celllab.backgroundColor = [UIColor colorWithRed:0.7 green:0.91 blue:0.26 alpha:1];
cell.celllab.textColor = [UIColor blackColor];
cell.blackarrow.hidden = YES;
cell.greenarrow.hidden = NO;
cell.bmwatch.hidden = YES;
cell.swiliam.hidden = NO;
cell.greenmsg.hidden = NO;
cell.whitemsg.hidden = YES;
}
else
{
cell.celllab.backgroundColor = [UIColor blackColor];
cell.celllab.textColor = [UIColor whiteColor];
cell.celllab.textAlignment = NSTextAlignmentLeft;
cell.greenarrow.hidden = YES;
cell.blackarrow.hidden = NO;
cell.bmwatch.hidden = NO;
cell.swiliam.hidden = YES;
cell.greenmsg.hidden = YES;
cell.whitemsg.hidden = NO;
}
return cell;
}
在您的 SendMessageTableViewCell class 中编写以下函数来更改其子视图的框架
- (void)layoutSubviews{
self.textLabel.frame = CGRectMake(rightPadding, topPadding,self.frame.size.width - (2*rightPadding), self.frame.size.height-(2*topPadding))
}
自定义 class 中的上述代码将在您的单元格内容子视图更改其框架时调用,届时您可以为标签添加填充,也可以更改其他内容框架。
它非常 simple.Just 将 uilabel 子类化并编写此方法
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
如何只为 UITableViewCell
中的标签从左到右添加填充?
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cellmessage";
SendMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[SendMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setAccessoryType:UITableViewCellAccessoryNone];
NSDictionary *dict = [self->serverArray objectAtIndex: indexPath.row];
cell.celllab.text= [dict objectForKey:@"message"];
CGFloat fixedWidth = cell.celllab.frame.size.width;
CGSize newSize = [cell.celllab sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = cell.celllab.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height + 10);
cell.celllab.frame = newFrame;
cell.celllab.layer.cornerRadius = 4.0f;
cell.celllab.layer.masksToBounds = YES;
//cell.celllab.layer.borderColor = [UIColor blackColor].CGColor;
//cell.celllab.layer.borderWidth = 10.0;
CAShapeLayer *shape = [CAShapeLayer layer];
shape.frame = cell.celllab.bounds;
//shape.path = maskPath.CGPath;
shape.lineWidth = 3.0f;
shape.strokeColor = [UIColor whiteColor].CGColor;
[cell.celllab.layer addSublayer:shape];
if([[dict objectForKey:@"type"] isEqualToString:@"u2a"])
{
cell.celllab.textAlignment = NSTextAlignmentRight;
cell.celllab.backgroundColor = [UIColor colorWithRed:0.7 green:0.91 blue:0.26 alpha:1];
cell.celllab.textColor = [UIColor blackColor];
cell.blackarrow.hidden = YES;
cell.greenarrow.hidden = NO;
cell.bmwatch.hidden = YES;
cell.swiliam.hidden = NO;
cell.greenmsg.hidden = NO;
cell.whitemsg.hidden = YES;
}
else
{
cell.celllab.backgroundColor = [UIColor blackColor];
cell.celllab.textColor = [UIColor whiteColor];
cell.celllab.textAlignment = NSTextAlignmentLeft;
cell.greenarrow.hidden = YES;
cell.blackarrow.hidden = NO;
cell.bmwatch.hidden = NO;
cell.swiliam.hidden = YES;
cell.greenmsg.hidden = YES;
cell.whitemsg.hidden = NO;
}
return cell;
}
在您的 SendMessageTableViewCell class 中编写以下函数来更改其子视图的框架
- (void)layoutSubviews{
self.textLabel.frame = CGRectMake(rightPadding, topPadding,self.frame.size.width - (2*rightPadding), self.frame.size.height-(2*topPadding))
}
自定义 class 中的上述代码将在您的单元格内容子视图更改其框架时调用,届时您可以为标签添加填充,也可以更改其他内容框架。
它非常 simple.Just 将 uilabel 子类化并编写此方法
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}