標籤:範圍 代碼 建立 ring contents origin round str bsp
(一)初始化
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;}
IOS代碼布局(六) UILabel