我們經(jīng)常會(huì)有這樣的需求:
在文字后面添加 視圖展示 標(biāo)簽或者按鈕“點(diǎn)擊查看”
解決方案:
獲取到這段文字的最后一個(gè)內(nèi)容所在的位置即它的坐標(biāo)负甸,只要知道了這個(gè)位置就可以在后面添加其他內(nèi)容
代碼思路:
//高固定寬不固定: 當(dāng)內(nèi)容為一行的時(shí)候:contentLabel所在的尺寸:
CGRect OneLineRect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:NULL];
CGSize oneLineSize = OneLineRect.size;
//寬固定高不固定: 當(dāng)內(nèi)容為多行的時(shí)候:
CGRect multipleLinesRect = [text boundingRectWithSize:CGSizeMake(kLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:NULL];
CGSize multipleLinesSize = multipleLinesRect.size;
通過(guò)兩種size對(duì)比獲取到相應(yīng)的位置:
CGPoint lastPoint ;
CGFloat width = 0.0;
CGFloat proY = 0.0;
//判斷是否多行顯示:
if (oneLineSize.width <= multipleLinesSize.width)
{
//內(nèi)容顯示1行:最后一個(gè)字所在的 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:@"%@", @"多行冊(cè)書數(shù)據(jù):若該內(nèi)容屬于機(jī)構(gòu)性質(zhì)的熊镣,則標(biāo)記“機(jī)構(gòu)”標(biāo)識(shí);若該內(nèi)容屬于個(gè)人所有,則標(biāo)記“個(gè)人”標(biāo)識(shí) "];
// text = @"1行測(cè)試數(shù)據(jù)";
_contentLabel.text = text;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18.0], NSFontAttributeName, nil];
//默認(rèn)label的高度:
CGFloat height = 25;
//高固定寬不固定: 當(dāng)內(nèi)容為一行的時(shí)候:
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;
}
//寬固定高不固定: 當(dāng)內(nèi)容為多行的時(shí)候:
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)
{
//內(nèi)容顯示1行:最后一個(gè)字所在的 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 = @"機(jī)構(gòu)";
_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 = @"個(gè)人";
_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