我有一个UITableView
我有不同大小的图像。所以当我运行程序时,我在 TableView 中得到了不同的标签起点。
我希望所有标签都从同一点开始。
对齐对我不起作用。我希望所有标签都从左侧一定距离开始。
有什么解决办法吗?
我有一个UITableView
我有不同大小的图像。所以当我运行程序时,我在 TableView 中得到了不同的标签起点。
我希望所有标签都从同一点开始。
对齐对我不起作用。我希望所有标签都从左侧一定距离开始。
有什么解决办法吗?
您的标签位置因图像而受到干扰?
如果是,那么有 2 种方法可以做到 1
[imgView setContentMode:UIViewContentModeScaleToFill]
2 你应该首先获取图像视图的宽度,然后添加一些空间因子,然后为你的 uilabel 设置适当的起点。
UILabel *lbl = ...;
UIImageView * imgView = ....;
CGRect lblFrame = lbl.frame;
lblFrame.origin.x = imgView.frame.origin.x + imgView.frame.size.width + 10;
[lbl setFrame:lblFrame];
编辑:
如果调整 uiimage 的大小不是问题,那么调整你的图像大小......
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage; }
块引用
> [CLASSNAME imageWithImage:yourImage scaledToSize:CGSizeMake(50,50)];
从 UITableViewCell 派生一个类。
覆盖方法layoutSubviews
。在此方法中,为所有组件(如 imageview、主标签等)设置适当的框架。
自定义单元格.h
@interface CustomCell : UITableViewCell
{
}
@end
CustomCell.m
@implementation CustomCell
-(void) layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = SOME_FRAME;
self.textLabel.frame = SOME_OTHER_FRAME;
}
@end
现在,不要从 cellForRowAtIndexPath 中的 UITableViewCell 创建单元格,而是使用上面的单元格。
试试这段代码,它对对齐 UiTableView 中的 UILabel 非常有用: UILabel *label=[[UILabel alloc]init];
label.frame=CGRectMake(80, 2, 180, 60);
label.backgroundColor=[UIColor clearColor];
label.text=[ShopListOfItems objectAtIndex:indexPath.row];
label.textColor=[UIColor whiteColor];
label.font=[UIFont boldSystemFontOfSize:20];
label.numberOfLines=2;
[cell.contentView addSubview:label];