2014-05-20 39 views
-1

我想要做的是,添加UIScrollView和內滾動UIView和內部UIView 2動態高度標籤。現在我想將UIScrollView的高度設置爲在頁面底部滾動。 但現在我正在無限滾動。任何人都可以幫助我使用這段代碼?如何獲得UIView的高度

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 155.0, 320, 513)]; 
UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(0.0, 155.0, 320.0, FLT_MAX)]; 

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 155.0, 320.0, FLT_MAX)]; 
lable.numberOfLines = 0; 
lable.text = @"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."; 
[lable sizeToFit]; 

UILabel *slabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 480.0, 320.0, FLT_MAX)]; 
slabel.numberOfLines = 0; 

slabel.text = @」 It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).」; 
[slabel sizeToFit]; 

scrollview.contentSize = CGSizeMake(scrollview.contentSize.width, myview.frame.size.height); 

[myview addSubview:lable]; 
[myview addSubview:slabel]; 
[scrollview addSubview:myview]; 
[self.view addSubview:scrollview]; 
+0

'FLT_MAX' ...?你真的希望從'3.40282347E + 38F'這個小值中獲得什麼? – holex

+0

把這樣一段代碼放在'viewDidLoad'或'viewWillAppear'中是正確的。你究竟在哪裏放置了這段代碼?最後,你在'scrollView'上設置了'contentOffset'嗎? – Pancho

+0

holex ...我是iOS的初學者,我正在努力完成這項工作。我在數據庫中有4列要在UIScrollView中顯示在UILable中。所以我想要做的是..做一個for循環,並在UILable的所有4列寫入數據,但問題是,我不知道所有4列將包含多少代碼行。 – user2001666

回答

0

你應該使用這個代碼我申請滾動型480的高度[正常iphone尺寸]和應用標籤字體HelveticaNeue粗體,

 UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 480)]; 

    UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0, 320.0, 400)]; 
    [myview setBackgroundColor:[UIColor grayColor]]; 

    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 320.0, FLT_MAX)]; 
    lable.numberOfLines = 0; 

    [lable setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0]]; 
    NSString *strText = @"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."; 
    float y = 0; 
    CGSize textViewSize = [strText sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0] 
            constrainedToSize:CGSizeMake(320, FLT_MAX) 
             lineBreakMode:NSLineBreakByWordWrapping]; 

    [lable setText:strText]; 
    [lable setFrame:CGRectMake(lable.frame.origin.x, lable.frame.origin.y, 320,roundf(textViewSize.height))]; 

    [myview addSubview:lable]; 

    y = lable.frame.origin.y+textViewSize.height; 
    UILabel *slabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, y, 320.0, FLT_MAX)]; 
    slabel.numberOfLines = 0; 
    [slabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0]]; 
    //the text of both label is same thats why we use same height used for first one 

    slabel.text = @"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."; 
    [slabel setText:strText]; 
    [slabel setFrame:CGRectMake(slabel.frame.origin.x, y, 320,roundf(textViewSize.height))]; 

    y = slabel.frame.origin.y+textViewSize.height; 

    [myview addSubview:slabel]; 
    [myview setFrame:CGRectMake(0,myview.frame.origin.y,myview.frame.size.width,y)];  
    scrollview.contentSize = CGSizeMake(scrollview.contentSize.width, myview.frame.size.height+15); 


    [scrollview addSubview:myview]; 
    [self.view addSubview:scrollview];