iphone - 创建一个自定义 View ,其中包含一个 UIImage,其中包含一个 UILabel(在图像底部)

标签 iphone xcode uiimageview custom-controls uilabel

An custom view with UIImage that have UILabel inside

我尝试制作一个像 BBC 新闻一样很酷的新闻应用程序。我认为每个新闻项(见图)里面必须有一个 UIImage 和 UILabel(新闻标题) --> 我们必须创建自定义 View 。

我已经在Apple Developer网站上阅读了如何创建自定义 View ,但它只是介绍,没有一些示例。

我的问题是: + 如何创建自定义 View (内部带有 UILabel 的 UIImage) + 如何将该 View 放入主屏幕应用程序的表格 View 中。

提前致谢。抱歉英语不好。

最佳答案

您可以通过多种方式完成此任务

1.您可以使用 UIImageView 和 UILabel 创建自定义 View ,并将其添加为 tableViewCell 中的 subview

2.您可以使用所需的标签和UIImageView创建自定义TableViewCell并直接使用。

使用 UIImageView 和 UILabel 创建自定义 View

右键单击“项目”->选择“新建文件”->选择“Objective C Class”->选择“UIView的子类”

自定义 View .h

@interface CustomView : UIView
{
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl ;
@end

自定义 View .m

@implementation CustomView

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl 
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UIImageView * imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 30)];
        [imageView1 setImage:img];
        [self addSubview:imageView1];
        [imageView1 release];

        UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 30, frame.size.width,30)];
        label1.text = lbl;
        label1.textColor = [UIColor blackColor];
        [self addSubview:label1];
        [label1 release];
    }
    return self;
}

@end

使用它

使用#import "CustomView.h"

导入CustomView
CustomView * cView = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) withImage:[UIImage imageNamed:@"img.png"] withLabel:@"Testasddddddddddddd"];
[self.view addSubview:cView];
[cView release];

关于iphone - 创建一个自定义 View ,其中包含一个 UIImage,其中包含一个 UILabel(在图像底部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274859/

相关文章:

ios - 由于应用程序中超过自动锁定时间,如何检测手机已被锁定?

ios - Swift - 从字符串中删除空格不起作用

iphone - Xcode 构建和资源文件夹

iphone - iPhone uiimageview 中的跑马灯效果

iphone - PhoneGap 2.7在状态栏中显示事件指示器

iphone - 我应该使用核心动画制作三点加载动画,还是应该找到一种方法来显示相同​​操作的 gif?

iphone - 在 iOS 中绘制矩形

ios - 图像被关闭后,UIImage 后面的模糊不会消失

ios - 使用 xcode 和 swift 在 IOS 中向下触摸/向上触摸

iphone - 我们如何将视频文件从库复制到文档目录?