// // KBKeyPreviewView.m // CustomKeyboard // #import "KBKeyPreviewView.h" #import "KBKey.h" @interface KBKeyPreviewView () @property (nonatomic, strong) UILabel *label; @property (nonatomic, strong) UIImageView *iconView; @end @implementation KBKeyPreviewView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor colorWithWhite:1 alpha:1.0]; self.layer.cornerRadius = 8.0; self.layer.masksToBounds = YES; self.layer.borderWidth = 0.5; self.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.15].CGColor; self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.3].CGColor; self.layer.shadowOpacity = 0.6; self.layer.shadowOffset = CGSizeMake(0, 2); self.layer.shadowRadius = 4.0; // _iconView = [[UIImageView alloc] initWithFrame:CGRectZero]; // _iconView.contentMode = UIViewContentModeScaleAspectFit; // _iconView.translatesAutoresizingMaskIntoConstraints = NO; // [self addSubview:_iconView]; _label = [[UILabel alloc] initWithFrame:CGRectZero]; _label.font = [UIFont systemFontOfSize:28 weight:UIFontWeightSemibold]; _label.textAlignment = NSTextAlignmentCenter; _label.textColor = [UIColor blackColor]; _label.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:_label]; // 预览只使用文本:尽量靠近顶部显示,上下留少量安全距离。 [NSLayoutConstraint activateConstraints:@[ [_label.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:4], [_label.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-4], [_label.topAnchor constraintEqualToAnchor:self.topAnchor constant:2], [_label.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-2], ]]; } return self; } - (void)configureWithKey:(KBKey *)key icon:(UIImage *)icon { NSString *text = key.output.length > 0 ? key.output : key.title; self.label.text = text; } @end