使用uilabel重新自调整高度后显示横线和竖线问题
这个使用uilabel自调节高度发现的问题,代码如下:
//content label
NSString *contentValue = ((MessageDetailRecord *)[self.entriesArray objectAtIndex:indexPath.row]).content;
CGFloat width = 0.0f;
CGSize orignalSize;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer]){
orignalSize = [contentValue sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f]}];
width = orignalSize.width;
}else{
width = [contentValue sizeWithFont:[UIFont systemFontOfSize:20.0f ]].width;
}
#pragma clang diagnostic pop
//set the most large width
if (width > cell.frame.size.width - 90.0f) {
width = cell.frame.size.width - 90.0f;
}
UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, 0.0f, width, 0.0f)];
self.messageContentLabel = content;
self.messageContentLabel.font = [UIFont systemFontOfSize:20.0];
self.messageContentLabel.backgroundColor = [UIColor clearColor];
self.messageContentLabel.numberOfLines = 0;
self.messageContentLabel.adjustsFontSizeToFitWidth = YES;
self.messageContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.messageContentLabel.backgroundColor = [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1.0];
CGRect labelFrameContent = self.messageContentLabel.frame;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer]) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:self.messageContentLabel.font forKey: NSFontAttributeName];
labelFrameContent.size = [contentValue boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
}else{
labelFrameContent.size = [contentValue sizeWithFont:self.messageContentLabel.font
constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode:self.messageContentLabel.lineBreakMode];
}
#pragma clang diagnostic pop
//adjust the height
if (labelFrameContent.size.height < 50.0f) {
labelFrameContent.size.height = 50.0f;
}
labelFrameContent.size.width += 10.0f;
if (labelFrameContent.size.width > cell.frame.size.width - 90.0f) {
labelFrameContent.size.width = cell.frame.size.width - 90.0f;
}
labelFrameContent.origin.x = 70.0f;
labelFrameContent.origin.y = self.cellDateLabel.frame.size.height + 10.0f;
self.messageContentLabel.frame = labelFrameContent;
[cell.contentView addSubview:self.messageContentLabel];
解决方法是使用uitextview来替换即可
UITextView *content = [[UITextView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, width, 0.0f)];
self.messageContentLabel = content;
[content release];
self.messageContentLabel.font = [UIFont systemFontOfSize:16.0f];
self.messageContentLabel.backgroundColor = [UIColor clearColor];
self.messageContentLabel.scrollEnabled = NO;
self.messageContentLabel.userInteractionEnabled = NO;
//self.messageContentLabel.numberOfLines = 0;
//self.messageContentLabel.adjustsFontSizeToFitWidth = YES;
// self.messageContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.messageContentLabel.backgroundColor = [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1.0];
self.messageContentLabel.text = contentValue;
self.messageContentLabel.textColor = [UIColor grayColor];
CGRect labelFrameContent = self.messageContentLabel.frame;
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer])
{
labelFrameContent.size = [contentValue boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}
context:nil].size;
}
else
{
labelFrameContent.size = [contentValue sizeWithFont:self.messageContentLabel.font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)];
}
#pragma clang diagnostic pop
注意:这个在ios 7.0以前的版本如果使用uitextview的话布局会有问题,如果使用uilabel的话在ios7.0上又会出现横竖线。所以最终的解决方案是区分当前的版本对待。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//delete the unnessary separator line
static NSString *CellIndentifier = @"MessageDetailList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
CGRect cellFrame =[cell frame];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (self.entriesArray &&
[self.entriesArray count] > 0) {
while ([cell.contentView.subviews lastObject] != nil)
{
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
}
UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width/4, 5.0f, cell.frame.size.width/2, 0.0f)];
self.cellDateLabel = dateLabel;
[dateLabel release];
self.cellDateLabel.text = ((MessageDetailRecord *)[self.entriesArray objectAtIndex:indexPath.row]).date;
self.cellDateLabel.textAlignment = NSTextAlignmentCenter;
self.cellDateLabel.font = [UIFont systemFontOfSize:14.0];
self.cellDateLabel.textColor = [UIColor grayColor];
self.cellDateLabel.backgroundColor = [UIColor clearColor];
self.cellDateLabel.numberOfLines = 0;
self.cellDateLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGRect labelFrameN = self.cellDateLabel.frame;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer]) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:self.cellDateLabel.font forKey: NSFontAttributeName];
labelFrameN.size = [self.cellDateLabel.text boundingRectWithSize:CGSizeMake(self.cellDateLabel.frame.size.width, CGFLOAT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
}else{
labelFrameN.size = [self.cellDateLabel.text sizeWithFont:self.cellDateLabel.font
constrainedToSize:CGSizeMake(self.cellDateLabel.frame.size.width, CGFLOAT_MAX)
lineBreakMode:self.cellDateLabel.lineBreakMode];
}
#pragma clang diagnostic pop
self.cellDateLabel.frame = labelFrameN;
[cell.contentView addSubview:self.cellDateLabel];
UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, self.cellDateLabel.frame.size.height + 10.0f,50.0f, 50.0f)];
self.imageView = icon;
self.imageView.image = [UIImage imageNamed:@"01_login_userdefaultavatar@2x.png"];
self.imageView.backgroundColor = [UIColor clearColor];
if (self.headImagesArray) {
[self.headImagesArray insertObject:self.imageView atIndex:indexPath.row];
}
[cell.contentView addSubview:[self.headImagesArray objectAtIndex:indexPath.row]];
[icon release];
//the content bg image
UIImageView *bgimageView = [[UIImageView alloc]initWithFrame:CGRectMake(65.0f, self.cellDateLabel.frame.size.height + 10.0f,cell.frame.size.width/3*2, 50.0f)];
self.messageContentbgImageView = bgimageView;
self.messageContentbgImageView.image = [UIImage imageNamed:@"04_message_messagebubble_gray@2x.png"];
self.messageContentbgImageView.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:self.messageContentbgImageView];
[bgimageView release];
//content label
NSString *contentValue = ((MessageDetailRecord *)[self.entriesArray objectAtIndex:indexPath.row]).content;
CGFloat width = 0.0f;
CGSize orignalSize;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer]){
orignalSize = [contentValue sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f]}];
width = orignalSize.width;
}else{
width = [contentValue sizeWithFont:[UIFont systemFontOfSize:20.0f ]].width;
}
#pragma clang diagnostic pop
//set the most large width
if (width > cell.frame.size.width - 90.0f) {
width = cell.frame.size.width - 90.0f;
}
CGRect labelFrameContent;
if ([GeneralInfo getDeviceSystemVer]) {
UITextView *content = [[UITextView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, width, 0.0f)];
self.messageContentLabel = content;
[content release];
self.messageContentLabel.font = [UIFont systemFontOfSize:16.0f];
self.messageContentLabel.backgroundColor = [UIColor clearColor];
self.messageContentLabel.scrollEnabled = NO;
self.messageContentLabel.userInteractionEnabled = NO;
self.messageContentLabel.backgroundColor = [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1.0];
self.messageContentLabel.text = contentValue;
self.messageContentLabel.textColor = [UIColor grayColor];
labelFrameContent = self.messageContentLabel.frame;
}
else
{
UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, 0.0f, width, 0.0f)];
self.messageContent = content;
[content release];
self.messageContent.font = [UIFont systemFontOfSize:20.0];
self.messageContent.backgroundColor = [UIColor clearColor];
self.messageContent.numberOfLines = 0;
self.messageContent.adjustsFontSizeToFitWidth = YES;
self.messageContent.lineBreakMode = NSLineBreakByWordWrapping;
self.messageContent.backgroundColor = [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1.0];
labelFrameContent = self.messageContent.frame;
}
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer])
{
labelFrameContent.size = [contentValue boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}
context:nil].size;
}
else
{
// labelFrameContent.size = [contentValue sizeWithFont:self.messageContentLabel.font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)];
labelFrameContent.size = [contentValue sizeWithFont:self.messageContent.font
constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode:self.messageContent.lineBreakMode];
}
#pragma clang diagnostic pop
/*#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([GeneralInfo getDeviceSystemVer]) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:self.messageContentLabel.font forKey: NSFontAttributeName];
labelFrameContent.size = [contentValue boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
}else{
labelFrameContent.size = [contentValue sizeWithFont:self.messageContentLabel.font
constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode:self.messageContentLabel.lineBreakMode];
}
#pragma clang diagnostic pop*/
//adjust the height
if (labelFrameContent.size.height < 50.0f) {
labelFrameContent.size.height = 50.0f;
}
labelFrameContent.size.width += 10.0f;
if (labelFrameContent.size.width > cell.frame.size.width - 90.0f) {
labelFrameContent.size.width = cell.frame.size.width - 90.0f;
}
labelFrameContent.origin.x = 70.0f;
labelFrameContent.origin.y = self.cellDateLabel.frame.size.height + 10.0f;
if ([GeneralInfo getDeviceSystemVer]){
self.messageContentLabel.frame = labelFrameContent;
[cell.contentView addSubview:self.messageContentLabel];
}else
{
self.messageContent.frame = labelFrameContent;
[cell.contentView addSubview:self.messageContent];
}
if (![GeneralInfo getDeviceSystemVer]){
UILabel *innerlabel = [[UILabel alloc]initWithFrame:CGRectMake(5.0f, 0.0f, self.messageContent.frame.size.width - 10.0f, self.messageContent.frame.size.height)];
innerlabel.textAlignment = NSTextAlignmentLeft;
innerlabel.font = [UIFont systemFontOfSize:16.0];
innerlabel.textColor = [UIColor grayColor];
innerlabel.backgroundColor = [UIColor clearColor];
innerlabel.text = contentValue;
innerlabel.numberOfLines = 0;
innerlabel.adjustsFontSizeToFitWidth = YES;
innerlabel.lineBreakMode = NSLineBreakByWordWrapping;
innerlabel.backgroundColor = [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1.0];
self.innerLabel = innerlabel;
[self.messageContent addSubview:innerlabel];
[innerlabel release];
}
//then the image should first check the type
NSString *type = ((MessageDetailRecord *)[self.entriesArray objectAtIndex:indexPath.row]).currentMessageType;
if ([type isEqualToString:@"self"])
{
//then readjust the frame
((UIImageView *)[self.headImagesArray objectAtIndex:indexPath.row]).frame = CGRectMake(cell.frame.size.width - 50.0f, self.cellDateLabel.frame.size.height + 10.0f, 40.0f, 40.0f);
self.imageView.frame = CGRectMake(cell.frame.size.width - 60.0f, self.cellDateLabel.frame.size.height + 10.0f, 50.0f, 50.0f);
self.messageContentbgImageView.image = [UIImage imageNamed:@"04_message_messagebubble_blue@2x.png"];
self.messageContentbgImageView.frame = CGRectMake(cell.frame.size.width - 65.0f - cell.frame.size.width/3*2, self.cellDateLabel.frame.size.height + 10.0f, cell.frame.size.width/3*2, 50.0f);
if ([GeneralInfo getDeviceSystemVer]){
self.messageContentLabel.backgroundColor = [UIColor colorWithRed:236/255.0f green:247/255.0f blue:253/255.0f alpha:1.0];
self.messageContentLabel.frame = CGRectMake(cell.frame.size.width - labelFrameContent.size.width - 70.0f, self.cellDateLabel.frame.size.height + 10.0f, labelFrameContent.size.width, labelFrameContent.size.height);
}else
{
self.messageContent.backgroundColor = [UIColor colorWithRed:236/255.0f green:247/255.0f blue:253/255.0f alpha:1.0];
self.messageContent.frame = CGRectMake(cell.frame.size.width - labelFrameContent.size.width - 70.0f, self.cellDateLabel.frame.size.height + 10.0f, labelFrameContent.size.width, labelFrameContent.size.height);
self.innerLabel.backgroundColor = [UIColor colorWithRed:236/255.0f green:247/255.0f blue:253/255.0f alpha:1.0];
}
}
if ([GeneralInfo getDeviceSystemVer]){
cellFrame.size.height = self.cellDateLabel.frame.size.height + self.messageContentLabel.frame.size.height + 20.0f;
}else
{
cellFrame.size.height = self.cellDateLabel.frame.size.height + self.messageContent.frame.size.height + 20.0f;
}
[cell setFrame:cellFrame];
self.record = [self.entriesArray objectAtIndex:indexPath.row];
if(!self.record.appIcon) {
if (self.localTableView.dragging == NO && self.localTableView.decelerating == NO)
{
//if has the valid image url then should add to the downloader list
NSString *startURL = self.record.friendHeadImageUrl;
NSLog(@"the starturl is %@",startURL);
if (startURL &&
(NSNull *)startURL != [NSNull null] &&
[startURL length] > 0) {
NSString *endURL = [NSURL URLWithString:startURL];
if (endURL) {
[self startIconDownload:self.record forIndexPath:indexPath];
}
}
}
}else{
//should add the new bg image
((UIImageView *)[self.headImagesArray objectAtIndex:indexPath.row]).image = [UIImage imageNamed:@"friend_head_image_bg"];
UIImageView *tmp = [[UIImageView alloc]initWithFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)];
tmp.image = self.record.appIcon;
[tmp setContentMode:UIViewContentModeScaleAspectFit];
[((UIImageView *)[self.headImagesArray objectAtIndex:indexPath.row]) addSubview:tmp];
[tmp release];
}
}
return cell;
}
使用uilabel重新自调整高度后显示横线和竖线问题的更多相关文章
- javascript超过容器后显示省略号效果(兼容一行或者多行)
javascript超过容器后显示省略号效果 在实际的项目中,由于文字内容的长度不确定性和页面布局的固定性,难免会出现文字内容超过div(或其他标签,下同)区域的情况,此时比较好的做法就是 ...
- css强制换行显示省略号之显示两行后显示省略号
1,首先来一个固定宽度,在一行显示,超出隐藏,显示省略号的样式 display:block; white-space:nowrap; overflow:hidden; text-overflow:el ...
- Advanced Office Password Recovery安装后显示是英文版的
一些才开始接触Advanced Office Password Recovery(即AOPR)的朋友,在安装Advanced Office Password Recovery的时候可能发现Advanc ...
- 微信支付 发布后显示 System:access_denied
微信支付发布后显示 System:access_denied (android)或 System:not_allow(IOS) 我们项目用的是.NET MVC3 授权目录是:http://mynetd ...
- JavaScript学习笔记-元素在滚动条滑动一定高度后自动置顶
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 解决mysql无法插入中文数据及插入后显示乱码的问题
(1)废话不多说就是使用mysql数据库的时候无法输入中文,可以输入中文后显示的又是乱码!! (2开始解决问题: 第一步:找到安装mysql的目录找到 my.ini 文件: 第二步:使用记事本打开my ...
- [转]装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core)
转:装完Centos7提示Initial setup of CentOS Linux 7 (core) 在用U盘装完CentOS后,重新开机启动后显示: Initial setup of Cent ...
- 装完RHEL7后,重新开机启动后显示:Initial setup of CentOS Linux 7 (core) 提示license报错
装完RHEL7后,重新开机启动后显示: 1) [x] Creat user 2) [!] License information (no user will be created) (license ...
- AngularJS 表单提交后显示验证信息与失焦后显示验证信息
虽然说AngularJS的实时表单验证非常有用,非常高效方便,但是当用户还没有完成输入时便弹出一个错误提示,这种体验是非常糟糕的. 正常的表单验证逻辑应该是在用户提交表单后或完成当前字段中的输入后,再 ...
随机推荐
- 更改ubuntu的官方镜像源
我们自己安装的ubuntu通常默认镜像源是官方的,并不好用,因为网速以及限制比较多,所以为了使用方便,通常都会去更改一下默认的镜像源配置. 这里我们使用清华大学开源镜像软件站,https://mirr ...
- PHP操作MySQL事务实例
PHP与MYSQL事务处理 一般来说,事务都应该具备ACID特征.所谓ACID是Atomic(原子性),Consistent(一致性),Isolated(隔离性),Durable(持续性)四个词的首字 ...
- 手动搭建redis集群(3台)
安装redis 1.搜索源中的redis包 apt-cache pkgnames | grep redis 2.安装redis-server apt-get install redis-server ...
- eclipse中新建maven项目无法添加src/main/java问题
eclipse创建maevn web项目,在选择maven_archetype_web原型后,默认只有src/main/resources这个Source Floder. 按照maven目录结构,添加 ...
- HDU 3506 DP 四边形不等式优化 Monkey Party
环形石子合并问题. 有一种方法是取模,而如果空间允许的话(或者滚动数组),可以把长度为n个换拓展成长为2n-1的直线. #include <iostream> #include <c ...
- [转]pickle python数据存储
python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...
- mysql条件查询and or使用实例及优先级介绍
mysql and与or介绍 AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来. 使用OR关键字时: 只要符合这几个查询条件的其中一个条件,这样的记录就会被查询出来. 如果不符合这 ...
- HDU-3065 病毒侵袭持续中 AC自动机又是一板子!
病毒侵袭持续中 上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数.这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了. 有 ...
- Distimo发布新SDK 帮助开发者跟踪应用下载转换率
著名应用分析机构Distimo近日刚刚发布了新的SDK,可以帮助开发者更好地跟踪应用下载转换率,同时也可以帮助开发者更好地了解多个在线营销活动的表现. Distimo发布的新工具叫做“Distimo ...
- 【bzoj3231】[Sdoi2008]递归数列 矩阵乘法+快速幂
题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj和 cj ...