2011-03-09 59 views

回答

0

,顯示一個類似iPhone的應用程序列表顯示畫面的屏幕,即圖像的標題,我不知道有任何控件提供的基本庫做你提到的。但是,簡單地對UIImageView進行子類化並不會很困難,因此它包含位於圖像正下方的UILabel。這可以通過很多方式完成,並不是很難。

一個基本的想法是這樣的:

頁眉:

@interface ImageTextView : UIImageView { 
    UILabel *title; 
} 

-(id) initWithFrame:(CGRect)_frame; 
-(void) setTitle:(NSString *)_title; 

@end 

實現:

@implementation ImageTextView 

-(id) initWithFrame:(CGRect)_frame{ 
    if (self = [super initWithFrame:_frame]){ 
     self.layer.masksToBounds = NO; 
     title = [[UILabel alloc] initWithFrame:(CGRect){0,_frame.size.height,_frame.size.width,50}]; 
     title.backgroundColor = [UIColor clearColor]; 
     title.textColor = [UIColor whiteColor]; 
     title.textAlignment = UITextAlignmentCenter; 
     [self addSubview:title]; 
    } 

    return self; 
} 

-(void) setTitle:(NSString *)_title{ 
    title.text = _title; 
} 

-(void) dealloc{ 
    [title release]; 
    [super dealloc]; 
} 

@end 

然後使用它你可以使用如下代碼:

ImageTextView *test = [[ImageTextView alloc] initWithFrame:(CGRect){0,0,100,100}]; 
test.image = [UIImage imageNamed:@"example.png"]; 
[test setTitle:@"TEST"]; 
[mainView addSubview:test]; 

Hope t他的幫助。

乾杯。

+0

你是對的,但個人(恕我直言)我會做一個包含UIImageView和UILabel,而不是將標籤內的圖像的xib。這是一個更多的努力,但你得到更大的靈活性與縮放/調整圖像/圖像和文本之間的分離等 – deanWombourne 2011-03-09 20:37:21

+0

@deanWombourne是的,你可以。就個人而言,儘管我以編程方式完成了我的所有開發,所以我不知道如何使用xib來實現這一點。你可以通過繼承UIView而不是UIImageView並添加UIImageView和UILabel來完成同樣的事情。這基本上會導致相同水平的靈活性。我發佈的方法稍微簡單一些。 – 2011-03-09 20:52:35

+0

你是對的 - 你的回答是最簡單的方式 - 我認爲當毗溼奴看到它會被接受:)你應該有一個界面建設者的遊戲,一旦你克服了可怕的界面古怪,你可以得到更快的反饋關於你的用戶界面如何看待。 – deanWombourne 2011-03-09 21:37:12