iOS 重写UITableViewCell之动态获取label文字的宽度进行布局

时间:2022-09-08 20:01:29

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文字的宽度进行布局的更多相关文章

  1. 根据文字动态计算Label高度或宽度

    //根据已知的label宽度计算文字高度 CGRect rect = [reson boundingRectWithSize:CGSizeMake(label_W, 0) options:NSStri ...

  2. swift 动态获取label宽度或高度

    func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let statusLabelText: NSS ...

  3. iOS根据文字字数动态确定Label宽高

    我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...

  4. iOS 根据文字字数动态确定Label宽高

    iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...

  5. 李洪强iOS开发之动态获取UILabel的bounds

    李洪强iOS开发之动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需 ...

  6. iOS&lowbar;根据文字字数动态确定Label宽高

    iOS7中用以下方法 CGSize 替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法 // iOS7_API_根据文字 字数动态确定Label宽高 ...

  7. iOS开发遇到的错误 -- Label显示多行文字导致宽度和高度的问题

    Label的宽度问题 注意:UILabel下面需要设置preferredMaxLayoutWidth ,设置了autolayout和numberofline的UIlabel才显示多行 label宽度的 ...

  8. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  9. iPhone&colon;动态获取UILabel的高度和宽度

    转自:http://www.cnblogs.com/spiritstudio/archive/2011/11/17/2252074.html 在使用UILabel存放字符串时,经常需要获取label的 ...

随机推荐

  1. 常用html、CSS、javascript前端命名规范

    无论是从技术角度还是开发视角,对于web前端开发规范文档都有一定规范,本文就css3和html5的发展前景总结了一系列的web开发文档,仅供大家参考. 规范目的: 为提高团队协作效率, 便于后台人员添 ...

  2. LAMP理论整理

    关于PHP 官网:http://www.php.net 一.PHP简介 PHP是通用服务器端脚本编程语言,其主要用于web开发以实现动态web页面,它也是最早实现将脚本嵌入HTML源码文档中的服务器端 ...

  3. U盘容量变小的处理方式

    我就说一下win7下的步骤吧,xp应该差不多,管理员身份运行命令行(快捷键win+R), diskpart回车 list disk回车 看看你的u盘是哪一个,选择他(select disk X回车) ...

  4. maven操作

    Java 编程入门(系列)      Python数据分析与挖掘经典案例实战      "我的2016"主题征文活动 关闭 maven仓库--私服(Nexus的配置使用) 2013 ...

  5. Zookeeper工作原理

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...

  6. 随机森林之oob error 估计

    摘要:在随机森林之Bagging法中可以发现Bootstrap每次约有1/3的样本不会出现在Bootstrap所采集的样本集合中,当然也就没有参加决策树的建立,那是不是意味着就没有用了呢,答案是否定的 ...

  7. 优化从 App&period;config 读取配置文件

    public class AppSettingsConfig { /// <summary> ////// </summary> public static int Query ...

  8. learning makefile set debug level and build command

  9. Partition Numbers的计算

    partition numbers的定义 A000041 就是将正整数n分为k(\(1\le k\le n)\)个正整数相加,即\(n=a_1+a_2+...+a_k\)且\(a_1\le a_2\l ...

  10. win10&plus;Theano&plus;GPU

    1. cuda + cudnn 首先还是要先安装GPU库,具体和caffe安装中一样. 2. Theano 为防止下载速度慢,配置清华镜像 conda config --add channels ht ...