标题:
类似搜索的历史记录字体排版(1)
[打印本页]
作者:
look_w
时间:
2019-3-4 21:05
标题:
类似搜索的历史记录字体排版(1)
一.在OC中
封装一个view
/**
* 初始化
* @param frame 布局的视图的frame
* @param aTitle 标题
* @param aArray 传入的内容(数组里面是字符串)
*/
- (instancetype)initWithFrame
CGRect)frame title
NSString *)aTitle array
NSArray *)aArray
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor lightGrayColor];
[self viewLayoutWithTitle:aTitle array:aArray];
}
return self;
}
//计算关键词按钮的位置
- (void)viewLayoutWithTitle
NSString *)aTitle array
NSArray *)aArray
{
if (![aArray isKindOfClass:[NSArray class]]) {
return;
}
//删除当前视图所有子视图
[self removeAllSubviews];
//title
UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 12, self.frame.size.width-12-12, 18)];
tipLabel.font = [UIFont boldSystemFontOfSize:15];
tipLabel.text = aTitle?
"";
[self addSubview:tipLabel];
float zX = 12;//记录按钮的X
float zY = 36;//记录按钮的Y
float zH = 30;//记录按钮的高
for (int i=0; i<aArray.count; i++) {
CGSize size = [aArray
boundingRectWithSize:CGSizeMake(self.frame.size.width - 12*3, 18) options
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes
{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
size.width +=18;//每个按钮的长度
//如果摆放的按钮大于屏幕宽 则另起一行
if ((zX+size.width+12) > self.frame.size.width) {
zY += zH+10;//10是按钮上下间的间隔
zX = 12;//初始值
}
//最后一个按钮离底部距离
if ((zY+zH)>self.frame.size.height) {
CGRect sViewRect = self.frame;
sViewRect.size.height += (10+zH); //加一个上下间隔和按钮高度
self.frame = sViewRect;
}
UIButton *buton = [UIButton buttonWithType:UIButtonTypeCustom];
buton.backgroundColor = [UIColor whiteColor];
buton.frame = CGRectMake(zX, zY, size.width, zH);
buton.tag = i;
[buton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[buton setTitle:aArray
forState:UIControlStateNormal];
buton.titleLabel.font = [UIFont systemFontOfSize:15];
buton.layer.cornerRadius = 9;
buton.layer.masksToBounds = YES;
buton.layer.borderWidth = 1;
buton.layer.borderColor = [UIColor lightGrayColor].CGColor;
[buton addTarget:self action
selector(buttonAction
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:buton];
zX += buton.frame.size.width+12;//12是按钮左右间的间隔
}
}
- (void)buttonAction
UIButton *)sender
{
NSLog(@"点击第%ld个按钮",(long)sender.tag);
}
- (void)removeAllSubviews
{
while (self.subviews.count) {
[self.subviews.lastObject removeFromSuperview];
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0