现在越来越多涉及到图片展示的应用都会加入标签功能,标签功能也越来越流行。想起以前做过的一个项目用到过这个功能,所以趁有时间,整理一下,仅供参考,不足之处还请多多指教。
1、标记界面
这个界面放一个UIImageView,宽高比例1:1。进入这个界面之前,图片建议是裁剪成1:1,这个比例有利于标签的展示。这个界面代码就不用展示了。
2、加标签按钮
我们项目中是将标签分为两类,文字标签和地理位置标签,基本上是相同的。按钮的布局可以参考我的,也可以自定义。
- (void)setupButton { CGFloat btnWH = 70 CGFloat btnMargin = 50 UIButton *textBtn = [[UIButton alloc] initWithFrame:CGRectMake((screenW - btnWH * 2 - btnMargin) * 0.5, -btnWH, btnWH, btnWH)]; [textBtn setImage:[UIImage imageNamed:@"addtag"] forState:UIControlStateNormal]; [textBtn setImage:[UIImage imageNamed:@"addtag_selected"] forState:UIControlStateSelected]; [textBtn addTarget:self action:@selector(clickText) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:textBtn]; self.textBtn = textBtn; UILabel *textLabel = [[UILabel alloc] init]; textLabel.frame = (CGRect){{textBtn.frame.origin.x, CGRectGetMaxY(textBtn.frame)}, textBtn.frame.size}; textLabel.text = @"标签"; textLabel.textAlignment = NSTextAlignmentCenter; textLabel.textColor = [UIColor whiteColor]; textLabel.font = [UIFont systemFontOfSize:12]; [self .view addSubview:textLabel]; self.textLabel = textLabel; UIButton *locationBtn = [[UIButton alloc] initWithFrame:CGRectMake(textBtn.frame.origin.x + btnWH + btnMargin, -btnWH, btnWH, btnWH)]; [locationBtn setImage:[UIImage imageNamed:@"addtaglocation"] forState:UIControlStateNormal]; [locationBtn setImage:[UIImage imageNamed:@"addtaglocation_selected"] forState:UIControlStateSelected]; [locationBtn addTarget:self action:@selector(clickLocation) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:locationBtn]; self.locationBtn = locationBtn; UILabel *locationLabel = [[UILabel alloc] init]; locationLabel.frame = (CGRect){{locationBtn.frame.origin.x, CGRectGetMaxY(locationBtn.frame)}, locationBtn.frame.size}; locationLabel.text = @"地点"; locationLabel.textAlignment = NSTextAlignmentCenter; locationLabel.textColor = [UIColor whiteColor]; locationLabel.font = [UIFont systemFontOfSize:12]; [self .view addSubview:locationLabel]; self.locationLabel = locationLabel; }
时间: 2024-10-13 14:27:42