(一)初始化
1、创建
UILabel *lblBid=[[UILabel alloc] initWithFrame:CGRectMake(90,5,rect.size.width-110,30)];
2、设置背景颜色
lblBid.backgroundColor=[UIColor clearColor];
3、设置标签内容
lblBid.text=_txtBid;
4、设置行数
lblPs.numberOfLines=0;
5、自适应布局(需要和4一起使用)
[lblPs sizeToFit];
6、加入视图
[self.view addSubview:lblPs];
(二)设置单行标签并可上下滑动(ScrollView + Label)
思路:其实就是设置scroll为单行,然后自适应label,再在set方法中将scrollview的实际大小设置为label的大小
UIScrollView *scrollViewPs = [[UIScrollView alloc] init]; scrollViewPs.frame = CGRectMake(lblAV.frame.origin.x, lblAV.frame.origin.y+lblAV.frame.size.height, lblBid.frame.size.width, lblBid.frame.size.height); // frame中的size指UIScrollView的可视范围 scrollViewPs.backgroundColor = [UIColor clearColor]; scrollViewPs.showsVerticalScrollIndicator = YES; scrollViewPs.scrollEnabled=YES; [bgClearView1 addSubview:scrollViewPs]; UILabel *lblPs=[[UILabel alloc] initWithFrame:CGRectMake(0,0,scrollViewPs.frame.size.width,100)]; lblPs.text=_txtPs; lblPs.numberOfLines=0; //自适应布局 步骤1 [lblPs sizeToFit]; //自适应布局 步骤2 [scrollViewPs addSubview:lblPs];
在txtPs的set方法中设置scrollview的大小与label一样
-(void)setTxtPs:(NSString *)txtPs{ scrollViewPs.contentSize = lblPs.frame.size; _txtPs=txtPs; }
时间: 2024-10-10 05:15:31