【问题标题】:How to get the height of UIView如何获取 UIView 的高度
【发布时间】:2014-07-08 11:42:28
【问题描述】:

我想要做的是,添加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];

【问题讨论】:

  • FLT_MAX...?你究竟希望从3.40282347E+38F 的小值中得到什么?
  • 放置这样一段代码的正确位置是viewDidLoadviewWillAppear。你到底把这段代码放在哪里了?最后,您在scrollView 上设置contentOffset 的位置在哪里?
  • holex ...我是iOS的初学者,我正在努力完成这项工作。我在数据库中有 4 列想要在 UIScrollView 中的 UILable 中显示。所以我想做的是.. 做一个 for 循环并在 UILable 中为所有 4 列写入数据,但问题是我不知道所有 4 列将包含多少行代码。
  • Panayot Panayotov 我已将此代码放在 viewDidLoad 中。

标签: ios iphone uiview uiscrollview


【解决方案1】:

您应该应用此代码,我应用 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    相关资源
    最近更新 更多