Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 如何在UIView中显示字符串而不是图像?_Ios_Objective C_Uiview - Fatal编程技术网

Ios 如何在UIView中显示字符串而不是图像?

Ios 如何在UIView中显示字符串而不是图像?,ios,objective-c,uiview,Ios,Objective C,Uiview,接口是这样定义的 @interface IGLDemoCustomView : UIView @property (nonatomic, strong) UIImage *image; @property (nonatomic, strong) NSString *title; @end @interface IGLDemoCustomView () @property (nonatomic, strong) UIImageView *imageView;

接口是这样定义的

@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
而实现文件如下所示

@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
当我从下拉菜单中选择图像时,它会显示在菜单中,但当我从下拉菜单中选择任何文本时,它不会显示在下拉列表视图中

任何线索都将不胜感激。 在视图中调用和设置字符串

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

就像你有
UIImageView
一样,你也需要在
UIView
中有一个
UILabel

@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;
}

您需要为标签设置框架

- (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

您好,我已经做了更改,但仍然不起作用。我已经编辑了上面的代码,并描述了如何调用它。请在这方面给予帮助