我们经常会有这样的需求:
在文字后面添加 视图展示 标签或者按钮“点击查看”
解决方案:
获取到这段文字的最后一个内容所在的位置即它的坐标,只要知道了这个位置就可以在后面添加其他内容
代码思路:
//高固定宽不固定: 当内容为一行的时候:contentLabel所在的尺寸:
CGRect OneLineRect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:NULL];
CGSize oneLineSize = OneLineRect.size;
//宽固定高不固定: 当内容为多行的时候:
CGRect multipleLinesRect = [text boundingRectWithSize:CGSizeMake(kLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:NULL];
CGSize multipleLinesSize = multipleLinesRect.size;
通过两种size对比获取到相应的位置:
CGPoint lastPoint ;
CGFloat width = 0.0;
CGFloat proY = 0.0;
//判断是否多行显示:
if (oneLineSize.width <= multipleLinesSize.width)
{
//内容显示1行:最后一个字所在的 x: label的x + 这行文字的宽度,y不变
lastPoint = CGPointMake(contentLabX + oneLineSize.width, _contentLabel.frame.origin.y);
width = oneLineSize.width;
proY = _contentLabel.frame.origin.y;
} else {
int oneLineW = (int)oneLineSize.width;
int multiLinew = (int)multipleLinesSize.width;
int value = oneLineW % multiLinew ;
NSLog(@"一行的宽度:%f 多行的宽度:%f 一行总宽度:%f",oneLineSize.width,multipleLinesSize.width,kLabelWidth);
lastPoint = CGPointMake(contentLabX + value , multipleLinesSize.height - oneLineSize.height );
width = kLabelWidth;
height = multipleLinesSize.height;
proY = multipleLinesSize.height - oneLineSize.height + _contentLabel.frame.origin.y;
}
lastPoint即为需要的位置。
一行的效果:
多行的效果:
完整代码:
import "ViewController.h"
define kWidth ([[UIScreen mainScreen] bounds].size.width)
define kLabelWidth (kWidth - 30)
@interface ViewController ()
@property(nonatomic, strong) UILabel * contentLabel;
@property(nonatomic, strong) UILabel * proLab;
@property(nonatomic, strong) UILabel * personaLab;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat contentLabX = 15;
_contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentLabX, 80, kLabelWidth, 25)];
_contentLabel.font = [UIFont systemFontOfSize:18.0];
_contentLabel.backgroundColor = [UIColor lightGrayColor];
_contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
_contentLabel.numberOfLines = 0;
[self.view addSubview:_contentLabel];
NSString * text = [NSString stringWithFormat:@"%@", @"多行册书数据:若该内容属于机构性质的,则标记“机构”标识;若该内容属于个人所有,则标记“个人”标识 "];
// text = @"1行测试数据";
_contentLabel.text = text;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18.0], NSFontAttributeName, nil];
//默认label的高度:
CGFloat height = 25;
//高固定宽不固定: 当内容为一行的时候:
CGRect OneLineRect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:NULL];
CGSize oneLineSize = OneLineRect.size;
if (oneLineSize.height < height) {
oneLineSize.height = height;
}
//宽固定高不固定: 当内容为多行的时候:
CGRect multipleLinesRect = [text boundingRectWithSize:CGSizeMake(kLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:NULL];
CGSize multipleLinesSize = multipleLinesRect.size;
CGPoint lastPoint ;
CGFloat width = 0.0;
CGFloat proY = 0.0;
//判断是否折行:
if (oneLineSize.width <= multipleLinesSize.width)
{
//内容显示1行:最后一个字所在的 x: label的x + 这行文字的宽度,y不变
lastPoint = CGPointMake(contentLabX + oneLineSize.width, _contentLabel.frame.origin.y);
width = oneLineSize.width;
proY = _contentLabel.frame.origin.y;
} else {
int oneLineW = (int)oneLineSize.width;
int multiLinew = (int)multipleLinesSize.width;
int value = oneLineW % multiLinew ;
NSLog(@"一行的宽度:%f 多行的宽度:%f 一行总宽度:%f",oneLineSize.width,multipleLinesSize.width,kLabelWidth);
lastPoint = CGPointMake(contentLabX + value , multipleLinesSize.height - oneLineSize.height );
width = kLabelWidth;
height = multipleLinesSize.height;
proY = multipleLinesSize.height - oneLineSize.height + _contentLabel.frame.origin.y;
}
CGFloat x = lastPoint.x;
_contentLabel.frame = CGRectMake(contentLabX, 80, width, height);
//添加需要的视图:
_proLab = [UILabel new];
_proLab.frame = CGRectMake(x + 20, proY , 40, 25);
_proLab.text = @"机构";
_proLab.textColor = [UIColor redColor];
_proLab.font = [UIFont systemFontOfSize:14.0];
_proLab.layer.masksToBounds = YES;
_proLab.textAlignment = NSTextAlignmentCenter;
_proLab.layer.borderColor = [UIColor redColor].CGColor;
_proLab.layer.borderWidth = 1.0;
[self.view addSubview:_proLab];
_personaLab = [UILabel new];
_personaLab.frame = CGRectMake(CGRectGetMaxX(_proLab.frame) + 10, proY , 40, 25);
_personaLab.text = @"个人";
_personaLab.textColor = [UIColor redColor];
_personaLab.font = [UIFont systemFontOfSize:14.0];
_personaLab.layer.masksToBounds = YES;
_personaLab.textAlignment = NSTextAlignmentCenter;
_personaLab.layer.borderColor = [UIColor blueColor].CGColor;
_personaLab.layer.borderWidth = 1.0;
[self.view addSubview:_personaLab];
// Do any additional setup after loading the view, typically from a nib.
}
@end