【问题标题】:iphone: how to add text above and below the image in the scrollvew?iphone:如何在滚动视图中的图像上方和下方添加文本?
【发布时间】:2011-09-28 21:52:35
【问题描述】:

我想在图像上方添加页码,例如“1 of 50”,并在滚动视图中的图像下方添加图像描述。

我看过这个LINK,但仍然不知道如何实现它。

这是我的图像滚动视图示例代码。我试图找到一个可以添加文本和滚动图像的示例

NSUInteger i;
        for (i = 1; i <= kNumImages; i++)
        {
                NSString *imageName = [NSString stringWithFormat:@"images%d.jpg", i];
                UIImage *image = [UIImage imageNamed:imageName];
                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

                // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
                CGRect rect = imageView.frame;
                rect.size.height = kScrollObjHeight;
                rect.size.width = kScrollObjWidth;
                imageView.frame = rect;
                imageView.tag = i;  // tag our images for later use when we place them in serial fashion
                [scrollView1 addSubview:imageView];
                [imageView release];
        }

更新:

const CGFloat kScrollObjHeight  = 320;
const CGFloat kScrollObjWidth   = 280.0;
const NSUInteger kNumImages     = 50;


- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
                CGRect frame = view.frame;
                frame.origin = CGPointMake(curXLoc, 0);
                view.frame = frame;

                curXLoc += (kScrollObjWidth);
        }
}
- (void)viewDidLoad {

        self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

        // 1. setup the scrollview for multiple images and add it to the view controller
        //
        // note: the following can be done in Interface Builder, but we show this in code for clarity
        [scrollView1 setBackgroundColor:[UIColor blackColor]];
        [scrollView1 setCanCancelContentTouches:NO];
        scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
        scrollView1.scrollEnabled = YES;

        // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
        // if you want free-flowing scroll, don't set this property.
        scrollView1.pagingEnabled = YES;

        // load all the images from our bundle and add them to the scroll view
        NSUInteger i;
        for (i = 1; i <= kNumImages; i++)
        {
                NSString *imageName = [NSString stringWithFormat:@"creative%d.jpg", i];
                UIImage *image = [UIImage imageNamed:imageName];
                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

                // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
                CGRect rect = imageView.frame;
                rect.size.height = kScrollObjHeight;
                rect.size.width = kScrollObjWidth;
                imageView.frame = rect;
                imageView.tag = i;  // tag our images for later use when we place them in serial fashion
                [scrollView1 addSubview:imageView];
                [imageView release];
        }

        [self layoutScrollImages];

我想放这个,但找不到正确的位置或正确的偏移量显示在顶部

UILabel * topLabel = [[UILabel alloc] init];
            topLabel.text = [NSString stringWithFormat:@"%d of %d", i, kNumImages];
            rect.origin.x = offset;
            rect.size.height = 30; // however large you need the label to be
            topLabel.frame = rect;
            offset += 30;

【问题讨论】:

    标签: iphone image text label scrollview


    【解决方案1】:

    我认为这样的事情会起作用:

        float offset = 0;
        for (NSUInteger i = 1; i <= kNumImages; i++)
        {
                // load up the image
                NSString *imageName = [NSString stringWithFormat:@"images%d.jpg", i];
                UIImage *image = [UIImage imageNamed:imageName];
                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
                // image.size is important here and used to determine placement
                CGRect rect = imageView.frame;
                rect.size = image.size;
    
                // create top label, just basic will need to configure other settings like font
                UILabel * topLabel = [[UILabel alloc] init];
                topLabel.text = [NSString stringWithFormat:@"%d of %d", i, kNumImages];
                rect.origin.x = offset;
                rect.size.height = 30; // however large you need the label to be
                topLabel.frame = rect;
                offset += 30; // adding the label height
    
                // set image frame since we now know the location below top label            
                rect.size = image.size;
                rect.origin.x += offset;
                imageView.frame = rect;
                imageView.tag = i;
                offset += image.size.height; // adding image height
    
                // add bottom label below image
                UILabel * bottomLabel = [[UILabel alloc] init];
                bottomLabel.text = imageName; // just a sample description
                rect.origin.x += offset;
                rect.size.height = 30; // however large you need the label to be
                bottomLabel.frame = rect;
                offset += 30; // adding the label height
    
                [scrollView1 addSubview:topLabel];
                [scrollView1 addSubview:imageView];
                [scrollView1 addSubview:bottomLabel];
                [imageView release];
                [topLabel release];
                [bottomLabel release];
    
                offset += 20; // arbitrary spacing between images
        }
    

    【讨论】:

    • 在我尝试之前,有没有办法可以让描述差异。每个图像?或者更好的是,如果没有循环和滑动差异会更好。带有图像描述链接的图像(差异。名称文件)?我仍在尝试为此寻找样本,这将是更好更简单的方法...
    • 我可以查看未声明的偏移量示例吗?我也可以看到 topLabel.text 的示例 =(描述示例)谢谢!
    • @merril 不确定您在第一条评论中的意思,这会构建带有描述的图像的垂直列表,您可以通过使用偏移量作为 Y 轴而不是 X 轴来水平地执行相同操作。偏移量也从 0 开始,您可以根据需要进行设置。而 topLabel 是您的“x of x”文本,您必须提出描述,因为我没有看到任何提及可能是什么的。可以放bottomLabel.text = imageName(啊哈刚刚看到一个复制粘贴错误,会修复它。)
    • 图像在循环中,我必须将所有图像文件重命名为 image1、image2 等。我可以对页码执行相同操作,但描述文本(底部标签)如何使它不同.每个图像的描述而不使用循环。顺便问一下,你修了哪些?谢谢!
    • 我发现这是我发布的错误链接,我刚刚更新并更改了我实际引用的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多