iOS 重写UITableViewCell之动态获取label文字的宽度进行布局
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "MovieHomeController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[MovieHomeController alloc] init]]; self.window.rootViewController = navi; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface MovieHomeController : UIViewController @end
#import "MovieHomeController.h"
#import "CinemaInfomationCell.h" //#import "SGFocusImageFrame.h" #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define IS_IOS7 (IOS_VERSION >= 7.0 ? YES : NO) #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define scale SCREEN_WIDTH/320.0
#define advertScrollView_height 100*scale @interface MovieHomeController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
// UIScrollView *_advertScrollView;
// NSMutableArray *advertImages;
UITableView *_tableView; NSMutableArray *datas;
}
//@property(nonatomic, strong) SGFocusImageFrame *bannerView;//banner栏
@end @implementation MovieHomeController - (void)viewDidLoad {
[super viewDidLoad];
self.title = @"电影"; datas = [[NSMutableArray alloc] init];
NSArray *arr = @[@"哈艺时尚影城(白云YH城店)",@"20%",@"",@"广州市白云区广州大道北28号梅花园商业中心302",@"6.2km",@"",@"座",@"主题",@"满"];//立开
[datas addObject:arr]; self.view.backgroundColor = [UIColor lightGrayColor];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView]; } #pragma mark -- tableView 的数据源配置 --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return datas.count;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
CinemaInfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[CinemaInfomationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSArray *info = datas[indexPath.row];
cell.cinemaName.text = info[];
cell.rebateAccount.text = [NSString stringWithFormat:@"返%@",info[]];
cell.score.text = [NSString stringWithFormat:@"%@分",info[]];
cell.address.text = info[];
cell.distance.text = info[];
cell.price.text = [NSString stringWithFormat:@"¥ %@",info[]];
cell.style.text = info[];
cell.theme.text = info[];
cell.seat.text = info[];
// NSLog(@"******%@",info[0]);
return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
#import <UIKit/UIKit.h> @interface CinemaInfomationCell : UITableViewCell @property (nonatomic, strong) UILabel *cinemaName;//电影院名称
@property (nonatomic, strong) UILabel *rebateAccount;//返利金
@property (nonatomic, strong) UILabel *score; //等级评分
@property (nonatomic, strong) UILabel *address;//地址
@property (nonatomic, strong) UILabel *price;//价格
@property (nonatomic, strong) UILabel *distance;//距离
@property (nonatomic, strong) UILabel *seat;//座
@property (nonatomic, strong) UILabel *theme;//主题
@property (nonatomic, strong) UILabel *style;//满或其它的 @end
#import "CinemaInfomationCell.h" #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define top_height 5
#define cinemaName_height 30
#define rebateAccount_height 17
#define address_height 30
#define price_height 30
@interface CinemaInfomationCell ()
{
UIView *line;//底部的细线
UILabel *label;//起字
UIView *verticalLine1;//竖线
UIView *verticalLine2;//竖线
}
@end @implementation CinemaInfomationCell - (void)awakeFromNib {
// Initialization code
} - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.cinemaName = [[UILabel alloc] init];
[self addSubview:self.cinemaName]; self.rebateAccount = [[UILabel alloc] init]; self.rebateAccount.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
self.rebateAccount.font = [UIFont systemFontOfSize:];
self.rebateAccount.textColor = [UIColor whiteColor];
self.rebateAccount.textAlignment = NSTextAlignmentCenter;
self.rebateAccount.layer.cornerRadius = ;
self.rebateAccount.clipsToBounds = YES;
[self addSubview:self.rebateAccount]; self.score = [[UILabel alloc] init];
self.score.layer.borderColor = [[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:] CGColor];
self.score.layer.borderWidth = 1.5;
self.score.font = [UIFont systemFontOfSize:];
self.score.textColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
self.score.layer.cornerRadius = ;
self.score.clipsToBounds = YES;
self.score.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.score]; self.address = [[UILabel alloc] init];
self.address.textColor = [UIColor grayColor];
self.address.font = [UIFont systemFontOfSize:];
self.address.backgroundColor = [UIColor clearColor];
[self addSubview:self.address]; self.distance = [[UILabel alloc] init];
self.distance.textColor = [UIColor grayColor];
self.distance.font = [UIFont systemFontOfSize:];
self.distance.backgroundColor = [UIColor clearColor];
self.distance.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.distance]; self.price = [[UILabel alloc] init];
self.price.backgroundColor = [UIColor clearColor];
self.price.font = [UIFont systemFontOfSize:];
self.price.textColor = [UIColor colorWithRed:/255.0 green: blue:/255.0 alpha:1.0];
self.price.textAlignment = NSTextAlignmentRight;
[self addSubview:self.price]; //起字
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:/255.0 green: blue:/255.0 alpha:1.0];
label.text = @" 起";
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:];
[self addSubview:label]; self.style = [[UILabel alloc] init];
self.style.backgroundColor = [UIColor clearColor];
self.style.font = [UIFont systemFontOfSize:];
self.style.textColor = [UIColor grayColor];
[self addSubview:self.style]; verticalLine1 = [[UIView alloc] init];
verticalLine1.backgroundColor = [UIColor grayColor];
[self addSubview:verticalLine1]; self.theme = [[UILabel alloc] init];
self.theme.backgroundColor = [UIColor clearColor];
self.theme.font = [UIFont systemFontOfSize:];
self.theme.textColor = [UIColor grayColor];
self.theme.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.theme]; verticalLine2 = [[UIView alloc] init];
verticalLine2.backgroundColor = [UIColor grayColor];
[self addSubview:verticalLine2]; self.seat = [[UILabel alloc] init];
self.seat.backgroundColor = [UIColor clearColor];
self.seat.font = [UIFont systemFontOfSize:];
self.seat.textColor = [UIColor grayColor];
self.seat.textAlignment = NSTextAlignmentRight;
[self addSubview:self.seat]; //底部的细线
line = [[UIView alloc] init];
line.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
[self addSubview:line]; }
return self;
} - (void)layoutSubviews{
[super layoutSubviews];
//计算文字的宽度
self.cinemaName.numberOfLines = ;
UIFont *font = [UIFont fontWithName:@"Arial" size:];
self.cinemaName.font = font;
CGSize constraint = CGSizeMake(self.frame.size.width - , );
CGSize size = [self.cinemaName.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"===%@==%f",self.cinemaName.text,size.width);
self.cinemaName.frame = CGRectMake(, top_height, size.width+,cinemaName_height); self.rebateAccount.frame = CGRectMake(CGRectGetMaxX(self.cinemaName.frame), CGRectGetMidY(self.cinemaName.frame) - rebateAccount_height/2.0, , rebateAccount_height); self.score.frame = CGRectMake(CGRectGetMaxX(self.rebateAccount.frame)+, self.rebateAccount.frame.origin.y, , rebateAccount_height); self.address.frame = CGRectMake(, CGRectGetMaxY(self.cinemaName.frame), SCREEN_WIDTH - , address_height); self.distance.frame = CGRectMake(SCREEN_WIDTH - , self.address.frame.origin.y, , address_height); self.price.frame = CGRectMake(, CGRectGetMaxY(self.address.frame), , price_height); label.frame = CGRectMake(CGRectGetMaxX(self.price.frame), self.price.frame.origin.y+, , price_height); self.style.frame = CGRectMake(SCREEN_WIDTH - , CGRectGetMaxY(self.address.frame), , price_height); verticalLine1.frame = CGRectMake(CGRectGetMinX( self.style.frame) - , CGRectGetMidY(self.style.frame)-/2.0, , ); self.theme.frame = CGRectMake(CGRectGetMinX(verticalLine1.frame)--, CGRectGetMinY(self.style.frame), , price_height); verticalLine2.frame = CGRectMake(CGRectGetMinX(self.theme.frame)-, CGRectGetMidY(self.style.frame)-/2.0, , ); self.seat.frame = CGRectMake(CGRectGetMinX(verticalLine2.frame)--, CGRectGetMinY(self.style.frame), , price_height); line.frame = CGRectMake(,self.frame.size.height - ,SCREEN_WIDTH,);
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
// [super setSelected:selected animated:animated];
// Configure the view for the selected state
} - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
} @end
iOS 重写UITableViewCell之动态获取label文字的宽度进行布局的更多相关文章
- 根据文字动态计算Label高度或宽度
//根据已知的label宽度计算文字高度 CGRect rect = [reson boundingRectWithSize:CGSizeMake(label_W, 0) options:NSStri ...
- swift 动态获取label宽度或高度
func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let statusLabelText: NSS ...
- iOS根据文字字数动态确定Label宽高
我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...
- iOS 根据文字字数动态确定Label宽高
iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...
- 李洪强iOS开发之动态获取UILabel的bounds
李洪强iOS开发之动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需 ...
- iOS_根据文字字数动态确定Label宽高
iOS7中用以下方法 CGSize 替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法 // iOS7_API_根据文字 字数动态确定Label宽高 ...
- iOS开发遇到的错误 -- Label显示多行文字导致宽度和高度的问题
Label的宽度问题 注意:UILabel下面需要设置preferredMaxLayoutWidth ,设置了autolayout和numberofline的UIlabel才显示多行 label宽度的 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iPhone:动态获取UILabel的高度和宽度
转自:http://www.cnblogs.com/spiritstudio/archive/2011/11/17/2252074.html 在使用UILabel存放字符串时,经常需要获取label的 ...
随机推荐
- SwfUpload学习记录
参考资料: SWFUpload 2.5.0版 官方说明文档 中文翻译版 了解SWFUpload 多文件上传配置详解 WEB版一次选择多个文件进行批量上传(swfupload)的解决方案 jQuery轻 ...
- Mesa10.2在Win7上的编译
Mesa10仍然支持Windwos/linux,但是编译方式已经不提供makefile或者workspace Building on windows requires several open-sou ...
- tomcat bug之部署应用的时候经常会发上startup failed due to previous errors
在tomcat上部署应用的时候经常会发上startup failed due to previous errors错误.这个错误产生以后经常会让人摸不到头脑.以下是几点查找经验: 1.web.xml文 ...
- UML 关系
1. 关联关系(association) 关联关系式是用一条直线表示的,如A—B.表示在一段时间内将多个类的实例连接在一起,关联关系描述了某个对象在一段时间内一直知道另一个对象的存在.在Rose中为了 ...
- go liteIDE
go liteIDE 1 COMM FILE package pricetable import ( "fmt" "math" "os" ...
- 使用JavaScript创建我的分页
把下面的方法放到一个js文件,页面引用他就行了 JavaScript function PageList(PageSize, PageIndex, TotalCount, ParList) { $(& ...
- FastReport的交叉表实际使用的一个例子
计算发行-->定义份数月表(打开)出现 PosFraisPaysInput选择时间段后,点击“打印”.这个设计表格,就是交叉表. 交叉表的特点是:数据库是一条一条并列的但是出来的结果却是:横向是 ...
- sphinx续5-主索引增量索引和实时索引
原文件地址:http://blog.itpub.net/29806344/viewspace-1400942/ 在数据库数据非常庞大的时候,而且实时有新的数据插入,如果我们不更新索引,新的数据就sea ...
- JNI字段描述符(转)
转载自http://fgsink.blog.163.com/blog/static/16716997020124310169911/ “([Ljava/lang/String;)V” 它是一种对函数返 ...
- 遍历ModelState中存储的错误信息
在服务器端验证中,有时我们添加了一个ModelError,然后还需要将该信息以JS的形式返回到客户端.如: [HttpPost] public ActionResult Index(LogOnMode ...