Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UIImageView中添加UILabel_Ios_Objective C_Uiimageview_Uilabel - Fatal编程技术网

Ios 如何在UIImageView中添加UILabel

Ios 如何在UIImageView中添加UILabel,ios,objective-c,uiimageview,uilabel,Ios,Objective C,Uiimageview,Uilabel,我的应用程序中有大图像。我动态创建它,并希望在此图像上添加UILabel: UIImageView *thumbnailImage; thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)]; UILabel *lblTitle = [[UILabel alloc] init]; lblTitle.text = @"SomeText"; lbl

我的应用程序中有大图像。我动态创建它,并希望在此图像上添加UILabel:

UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = @"SomeText"; 
lblTitle.frame = CGRectMake(xTitle, yTitle , titleWidth ,titleHeight);
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
[thumbnailImage addSubview:lblTitle];
但是
[thumbnailImage addSubview:UILabel]代码不起作用


有人知道吗?

thumbnailImage是一个UIView,因此您可以向其中添加子视图,而不是写行

[缩略图图像添加子视图:UILabel]

使用以下命令编辑它:

[thumbnailImage addSubview:lblTitle];

这将起作用。:)

最好用框架初始化标签

您应该向标签添加文本

您应该将
UILabel
对象(
lblTitle
)添加到imageview,而不是
UILabel
类型

通过这些更正,您的代码应该如下所示:

UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xTitle, yTitle , titleWidth ,titleHeight)];
lblTitle.textLabel.text = @"Your text";
[thumbnailImage addSubview:lblTitle];

我想在图片底部添加一些文本,比如新闻文章

所以我用下面的代码来解决它,如果有人有更好的想法,请分享

UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(xImage,imageHeight - titleHeight , titleWidth , titleHeight )];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xTitle, yTitle , titleWidth ,titleHeight)];
titleView.alpha = 0.3;
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
[self.view addSubview:thumbnailImage];
[self.view addSubview:titleView];
lblTitle.textLabel.text = @"SomeText";
首先,我使用以下代码行:

 [titleView addSubview:lblTitle]

但是
标题视图的alpha影响
lblTitle
并使其透明。因此,删除这一行后,文本将变得清晰明亮

在哪里设置标签的文本?除非指定不透明的背景颜色,否则不会显示空标签。您正在添加UILabel而不是lblTitleUILabel,并为lable设置文本,因为它键入了misstake。我编辑它们。标签的框架是否在图像视图中?图幅位于其Superview的坐标中。我在代码中做了一些更正。事实上,我想在图片底部的透明区域添加一些文本,比如新闻文章。