[英]How to align the Label in UITableview?
我有一個UITableView
,其中有不同大小的圖像。 因此,當我運行程序時,我在TableView中得到的是標簽的不同起點。
我希望所有標簽都從同一點開始。
對齊對我不起作用。 我希望所有標簽從距左側一定距離開始。
有什么解決辦法嗎?
從UITableViewCell派生一個類。
覆蓋方法layoutSubviews
。 在此方法中,為所有組件(如imageview,primary label等)設置適當的幀。
CustomCell.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創建單元格。
您的標簽位置因圖像而受到干擾?
如果是,那么有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)];
嘗試以下代碼,在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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.