iOS UILabel根据文字获取高度及UITableCell动态获取高度(以截取快递信息为例)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "RootViewController.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]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" #define cellWidth [UIScreen mainScreen].bounds.size.width @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
{
// NSMutableArray *dateArr;
NSMutableArray *timeArr;// 时间
NSMutableArray *messsgeArr; // 物流信息
UITableView * _tableView;
}
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor lightGrayColor];
// dateArr = [[NSMutableArray alloc] init];
timeArr = [[NSMutableArray alloc] init];
messsgeArr = [[NSMutableArray alloc] init]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , cellWidth, [UIScreen mainScreen].bounds.size.height - ) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_tableView];
//去掉多余的cell
_tableView.tableFooterView = [[UIView alloc] init]; // 请求数据并进行处理
[self dealWithExpressMessage]; } - (void)dealWithExpressMessage{
NSString *htmlString = @"http://wap.kuaidi100.com/wap_result.jsp?rand=20120517&id=zhongtong&fromWeb=null&&postid=718969747957"; NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:htmlString] encoding:NSUTF8StringEncoding error:nil];
NSData *htmlData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; NSString *string = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
NSArray * array = [string componentsSeparatedByString:@"<p>·"];
// 分割后,获取有用的信息
NSMutableArray *mArr = [[NSMutableArray alloc] init];
for (int i = ; i < array.count; i++ ) {
if ((i != ) && (i != array.count - )) {
[mArr addObject:array[i]];
}
if (i == array.count -) {
NSArray *newArr = [array[i] componentsSeparatedByString:@"</form>"];
NSString *string = newArr[];
[mArr addObject:string];
}
}
// 将获取到的数据分开
for (NSString *subString in mArr) { NSArray *array = [subString componentsSeparatedByString:@"<br />"];
NSString *timeString = array[];
NSArray *dateArray = [timeString componentsSeparatedByString:@";"];
[timeArr addObject:dateArray[]];
/* 将年月日与时分秒的时间分开
NSArray *spaTimeArr = [timeString componentsSeparatedByString:@" "];
NSArray *dateArray = [spaTimeArr[0] componentsSeparatedByString:@";"];
// 获取年月日的时间
[dateArr addObject:dateArray[1]];
//获取时分秒的时间
[timeArr addObject:spaTimeArr[1]];
*/
// NSLog(@" == %@",dateArray[1]); NSString *address = array[];
NSArray *addressArr = [address componentsSeparatedByString:@"</p>"];
// 存放快递的信息
NSString *newString = [addressArr[] stringByReplacingOccurrencesOfString:@" " withString:@""];
[messsgeArr addObject:newString];
NSLog(@"==%@",newString);
}
// NSLog(@"===== %@",messsgeArr);
} #pragma mark -- tableView 的数据配置 --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return timeArr.count;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"cell";
tableView.separatorStyle = UITableViewCellSelectionStyleNone;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.userInteractionEnabled = NO;
for (UIView *subView in cell.subviews) {
[subView removeFromSuperview];
}
cell.backgroundColor = [UIColor lightGrayColor];
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , cellWidth, )];
timeLabel.textColor = [UIColor blueColor];
timeLabel.font = [UIFont systemFontOfSize:];
timeLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:timeLabel];
timeLabel.text = timeArr[indexPath.row]; UILabel *messageLabel = [[UILabel alloc] init];
[cell addSubview:messageLabel];
messageLabel.text = messsgeArr[indexPath.row]; messageLabel.numberOfLines = ;
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor = [UIColor blackColor];
UIFont *font = [UIFont fontWithName:@"Arial" size:];
messageLabel.font = font;
CGSize constraint = CGSizeMake(cellWidth - , );
CGSize size = [messageLabel.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// CGSize labelSize = [messageLabel.text boundingRectWithSize:boundSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
messageLabel.frame = CGRectMake(, , size.width,size.height);
CGRect rect = cell.frame;
rect.size.height = timeLabel.frame.size.height + messageLabel.frame.size.height + ;
cell.frame = rect; return cell;
} @end
iOS UILabel根据文字获取高度及UITableCell动态获取高度(以截取快递信息为例)的更多相关文章
- 获取iframe子页面内容高度给iframe动态设置高度
<!DOCTYPE html><html> <head> <meta charset="UTF-8" /> <meta nam ...
- 小程序TAB列表切换内容动态变化,scrollview高度根据内容动态获取
滑动tab选项卡 一.在小程序里面tab选项卡是用的是自带的swiper组件,下面直接上代码 <view class="container"> <view cla ...
- ios uilabel 根据文字 计算宽度 高度
//根据宽度求高度 + (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFl ...
- iOS UILabel 文字 置顶/置底 实现
iOS UILabel控件默认文字位置是居中的,如图所示: 但是我们经常碰到这样的需求,希望文字向上置顶,或者向下置底,但是很遗憾,iOS API中并没有提供相应的属性和方法,需要我们手动设置. 利用 ...
- 李洪强iOS开发之动态获取UILabel的bounds
李洪强iOS开发之动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需 ...
- IOS UI篇—UILabel的文字顶部对齐
UILabel的文字顶部对齐 NOV 20TH, 2011 默认UILabel是垂直居中对齐的,如果你的UILabel高度有多行,当内容少的时候,会自动垂直居中. 如下图所示(图片来自stackove ...
- iOS - UILabel
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UILabel : UIView <NSCoding> @available(iOS 2.0, *) p ...
- iOS UILabel自定义行间距
NSString *hintStr = @"输入材料标题搜索材料\n注:可根据材料序号直接搜索, 如TPO23"; CGSize size = [toolset returnTex ...
- 让UILabel的文字顶部对齐
参考资料 http://stackoverflow.com/questions/1054558/how-do-i-vertically-align-text-within-a-uilabel 方法一 ...
随机推荐
- nodejs:grunt使用合并压缩的基本使用
一.模块化历史 1,nodejs出现:主要解决后端js规范 2,commonjs:这个组织出来一些服务器规范 3,后端规范commonjs应用升级到前端commonjs2:cmd规范(seajs)和完 ...
- 2016.07.08,英语,《Vocabulary Builder》Unit 24
mand/mend comes from mandare, Latin for 'entrust' or 'order'. command and commandment: [kə'mændmənt] ...
- PHP 开启短标签
<?=STATIC_URL?> 让上面的语句可以正常运行,等同于下面的语句 <?php echo STATIC_URL;?> 可以在 php.ini 中找到 short_ope ...
- PROCESS STATES
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To understand the ope ...
- passing parameters by value is inefficient when the parameters represent large blocks of data
Computer Science An Overview _J. Glenn Brookshear _11th Edition_C Note that passing parameters by va ...
- 设置myeclipse 项目编码(UTF-8)
设置myeclipse开发项目默认编码为UTF-8Window-->Preferences-->General-->Workspace-->(Text file encodin ...
- 取消svn关联
把下面这段文字保存成一个reg文件 Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\sh ...
- docker squid---but git proxy should specify by git config --global http.proxy http:...
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.t ...
- FW nexus docker
原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy) ...
- HBase协处理器统计表数据量
1.Java代码实现 import org.apache.hadoop.hbase.client.coprocessor.AggregationClient; import org.apache.ha ...