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

时间:2016-12-30 11:44:34

标签: 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;
    @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;

2 个答案:

答案 0 :(得分:0)

就像您拥有UIImageView一样,您的UILabel内也需要UIView

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

答案 1 :(得分:0)

您需要为标签设置框架

// Create a custom view controller
    let PopUpVC = PopUpViewController(nibName: "PopUpViewController", bundle: nil)
 // Assign the data
    PopUpVC.data = popUpArray

如果您希望文字换行,则需要设置- (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设为0