2013-11-24 4 views
1

에 UILabel의를 추가하는 방법을 내가 내 응용 프로그램에서 큰 이미지가 있습니다. 나는 동적으로 생성하고 난이 이미지에 UILabel의를 추가 할 :있는 UIImageView

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]; 코드가 작동하지 않습니다.

어떤 아이디어가 있습니까? 당신이 파단를 추가 할 수 있도록

+0

어디에서 레이블의 텍스트를 설정합니까? 비 투명 배경색을 지정하지 않으면 빈 레이블이 표시되지 않습니다. – giorashc

+1

lblTitle – yurish

+0

UILabel 대신 UILabel을 추가하고 lable에 대한 텍스트를 설정하면 실수를 입력하는 중입니다. 내가 편집 해. – Aqil

답변

2

나는 뉴스 기사와 같은 그림의 하단에 텍스트를 추가 할.

그래서 아래 코드로 해결하면 더 좋은 아이디어가 있으면 공유하십시오.

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] 

하지만 titleView 영향 lblTitle의 알파하고 투명하게. 그래서이 줄을 삭제 한 후 lblTitle 텍스트가 선명하고 밝게 보입니다.

0

thumbnailImage은 다음과

[thumbnailImage addSubview:UILabel];

편집을 라인을 작성하는 it.instead하는 UIView의이다 ::

[thumbnailImage addSubview:lblTitle]; 

이 작동합니다. :)

0

레이블을 프레임으로 초기화하는 것이 좋습니다.

당신은 당신은 이미지 뷰 대신 UILabel 유형으로 UILabel 객체 (lblTitle)를 추가해야 라벨

에 텍스트를 추가해야합니다. 이러한 수정으로

, 코드는 다음과 같이한다 :

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]; 
+0

나는 코드에서 약간의 수정을했습니다. 사실 나는 그림의 하단에있는 투명한 영역에 뉴스 기사와 같은 텍스트를 추가하려고합니다. – Aqil

관련 문제