如何获得UIView的高度

时间:2014-05-20 09:29:17

标签: ios iphone uiview uiscrollview

我要做的是,在动态高度中添加UIScrollView和内部滚动UIView以及UIView 2内部标签。现在我想将UIScrollView的高度设置为在页面底部滚动。 但是现在我得到无限卷轴。任何人都可以帮我这个代码吗?

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];

1 个答案:

答案 0 :(得分:0)

你应该应用这个代码我应用scrollview 480的高度[对于正常的iphone大小]并应用标签字体HelveticaNeue-Bold,

     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];