iOS:如何在UIScrollView中显示长文本?

时间:2015-05-11 11:42:26

标签: ios objective-c uiscrollview uitextview

EDITED

我有一个UIScrollView,它顶部有一个ImageView,下面有一个UIlabel,下面我想放一个长文本。

我尝试将<a href="page2.php?item=n" onclick="document.forms['myform'].attributes.action.value = this.attributes.href.value; document.forms['myform'].submit(); return false;"> link </a> 放在UITextView内,但UIScrollView有自己的滚动条,我需要放一个长文本让UITextView做滚动而不是UIScrollView

我如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

我认为这样做可以做你正在尝试的事情。在我的代码中,我使用了 UILabel ,并根据 String long给它一个大小。 这是我的代码

//Add an scroll to full size frame to view
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//some properties of scroll
scroll.userInteractionEnabled = YES;
scroll.showsHorizontalScrollIndicator = YES;
[self.view addSubview:scroll];
//now add views into the scroll
UIImageView *myTopImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scroll.frame.size.width, 100)];
myTopImage.image = [UIImage imageNamed:@"your_image.png"];
UIFont *myFont = [UIFont fontWithName:@"Helvetica Neue" size:30];
[scroll addSubview:myTopImage];

NSString *myText = @"your  long text here";
//calculate width if it bigger than frame's width make it multiline
int width = [self calculateWidthForText:myText forHeight:30 forFont:myFont]+2;
if (width > scroll.frame.size.width) {
    width = scroll.frame.size.width;
    //calculate height for specific width
    int height = [self calculateHeightForText:myText forWidth:scroll.frame.size.width forFont:myFont]+2;
    //change your scroll contentSize
    scroll.contentSize = CGSizeMake(self.view.frame.size.width, myTopImage.frame.size.height + height);

    //now create your label
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, width, height)];
    myLabel.numberOfLines = 0;
    myLabel.text = myText;
    myLabel.textAlignment = NSTextAlignmentLeft;
    myLabel.font = myFont;
    [scroll addSubview:myLabel];

}else {

    //if it is smaller than width give it specific height and init
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, width, 30)];
    myLabel.numberOfLines = 0;
    myLabel.text = myText;
    myLabel.textAlignment = NSTextAlignmentLeft;
    myLabel.font = myFont;
    [scroll addSubview:myLabel];

}

这是我计算字符串大小的方法

- (CGFloat) calculateHeightForText:(NSString *)str forWidth:(CGFloat)width forFont:(UIFont *)font {
CGFloat result = 20.0f;
    if (str) {
        CGSize textSize = { width, 20000.0f };
        CGSize size = [str sizeWithFont:font constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
        result = MAX(size.height, 20.0f);
    }
    return result;
}

- (CGFloat) calculateWidthForText:(NSString *)str forHeight:(CGFloat)height forFont:(UIFont *)font {
CGFloat result = 20.0f;
    if (str) {
        CGSize textSize = { 20000.0f, height };
        CGSize size = [str sizeWithFont:font constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
        result = MAX(size.width, 20.0f);
    }
    return result;
}