2016-12-30 41 views
0

接口是這樣定義如何在UIView中顯示字符串而不是圖像?

@interface IGLDemoCustomView : UIView 
    @property (nonatomic, strong) UIImage *image; 
    @property (nonatomic, strong) NSString *title; 
@end 

雖然實現文件看起來像這樣

@interface IGLDemoCustomView() 

    @property (nonatomic, strong) UIImageView *imageView; 
    @property (nonatomic, strong) UILabel *titleLabel 
    @end 

    @implementation IGLDemoCustomView 

    - (instancetype)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 
      [self commonInit]; 
     } 
     return self; 
    } 

    - (instancetype)initWithCoder:(NSCoder *)aDecoder 
    { 
     self = [super initWithCoder:aDecoder]; 
     if (self) { 
      [self commonInit]; 
     } 
     return self; 
    } 

    - (void)commonInit 
    { 
     [self initView]; 
    } 

    - (void)initView 
{ 
    self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor; 
    self.layer.borderWidth = 2.0; 
    self.layer.masksToBounds = YES; 
    self.backgroundColor = [UIColor whiteColor]; 
    self.alpha = 0.8; 

    UIImageView *imageView = [[UIImageView alloc] init]; 
    imageView.contentMode = UIViewContentModeCenter; 
    [self addSubview:imageView]; 
    self.imageView = imageView; 
    UILabel *titleLabel; 
    self.titleLabel = [[UILabel alloc]init]; 
    titleLabel.center = self.center; // set proper frame for label 
    [self addSubview:titleLabel]; 
} 
    - (void)setImage:(UIImage *)image 
    { 
     _image = image; 
     self.imageView.image = image; 
    } 

    - (void)setString:(NSString *)title 
{ 
    self.title=title; 
    self.titleLabel.text = title; 
} 
    - (void)setFrame:(CGRect)frame 
    { 
     [super setFrame:frame]; 
     self.imageView.frame = self.bounds; 
     self.layer.cornerRadius = frame.size.height/2.0; 
    } 

    @end 

,當我從下拉菜單它顯示在菜單的下拉菜單中選擇圖像,但是當我從選擇任何文本下拉菜單它不顯示在下拉列表視圖。

任何線索將不勝感激。 呼叫和你一樣是有UIImageView你需要有一個UILabelUIView裏面太視圖

IGLDropDownItem *menuButton = strongSelf.dropDownMenu.menuButton; 
     IGLDemoCustomView *buttonView = (IGLDemoCustomView*)menuButton.customView; 
     buttonView.title = device.name; 

回答

0

您需要設置一個幀標籤

- (void)setFrame:(CGRect)frame 
{ 
    [super setFrame:frame]; 
    self.imageView.frame = self.bounds; 
    self.titleLabel.frame = self.bounds; 
    self.layer.cornerRadius = frame.size.height/2.0; 
} 

如果你期待的文字換行,你就需要設置lineBreakMode並設置numberOfLines爲0

0

設置字符串。

@interface IGLDemoCustomView() 

@property (nonatomic, strong) UIImageView *imageView; 
@property (nonatomic, strong) UILabel *titleLabel; 

@end 

- (void)initView 
{ 
    self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor; 
    self.layer.borderWidth = 2.0; 
    self.layer.masksToBounds = YES; 
    self.backgroundColor = [UIColor whiteColor]; 
    self.alpha = 0.8; 

    UIImageView *imageView = [[UIImageView alloc] init]; 
    imageView.contentMode = UIViewContentModeCenter; 
    [self addSubview:imageView]; 
    self.imageView = imageView; 

    self.titleLabel = [[UILabel alloc]init]; 
    titleLabel.center = self.center; // set proper frame for label 
    [self addSubview:titleLabel] 
} 

而且在setString

- (void)setString:(NSString *)title 
{ 
    self.title=title; 
    self.titleLabel.text = title; 
} 
+0

嗨,我已經做了更改,但仍然無法正常工作。我編輯了上面的代碼,並描述我如何調用它。請在這方面幫助 –

相關問題