ANA
ANA

Reputation: 280

iOS: how to display a long text in a UIScrollView?

EDITED

I have a UIScrollView, it has an ImageView at the top, under that there is a UIlabel, and under that i want to put a long text.

I tried to put a UITextView inside of a UIScrollView, but UITextView has its own scroll bar, i need to put a long text and let the UIScrollView do the scrolling not the UITextView.

How can i achieve that ?

Upvotes: 1

Views: 2192

Answers (1)

Gürhan KODALAK
Gürhan KODALAK

Reputation: 580

I think this way is ok to do what you are trying. In my code I used UILabel and gave it a size according to String long. and here is my code

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

}

and here is my methods to calculate string size

- (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;
}

Upvotes: 2

Related Questions