本文主要是介绍自定义键盘工具条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#import <UIKit/UIKit.h>typedef enum {HMComposeToolbarButtonTypeCamera, // 照相机HMComposeToolbarButtonTypePicture, // 相册HMComposeToolbarButtonTypeMention, // 提到@HMComposeToolbarButtonTypeTrend, // 话题HMComposeToolbarButtonTypeEmotion // 表情
} HMComposeToolbarButtonTypes;@class HMComposeToolbar;@protocol HMComposeToolbarDelegate <NSObject>@optional
- (void)composeTool:(HMComposeToolbar *)toolbar didClickedButton:(HMComposeToolbarButtonTypes)buttonType;@end@interface HMComposeToolbar : UIView
@property (nonatomic, weak) id<HMComposeToolbarDelegate> delegate;
/*** 设置某个按钮的图片** @param image 图片名* @param buttonType 按钮类型*/
//- (void)setButtonImage:(NSString *)image buttonType:(HMComposeToolbarButtonType)buttonType;/*** 是否要显示表情按钮*/
@property (nonatomic, assign, getter = isShowEmotionButton) BOOL showEmotionButton;
@end
#import "HMComposeToolbar.h"
#import "UIImage+Extension.h"
#import "UIView+WLFrame.h"
#import "UIView+Extension.h"
@interface HMComposeToolbar()
@property (nonatomic, weak) UIButton *emotionButton;
@end@implementation HMComposeToolbar- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];// 添加所有的子控件[self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:HMComposeToolbarButtonTypeCamera];[self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:HMComposeToolbarButtonTypePicture];[self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:HMComposeToolbarButtonTypeMention];[self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:HMComposeToolbarButtonTypeTrend];self.emotionButton = [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:HMComposeToolbarButtonTypeEmotion];}return self;
}/**键盘切换*/
- (void)setShowEmotionButton:(BOOL)showEmotionButton
{_showEmotionButton = showEmotionButton;if (showEmotionButton) { // 显示表情按钮[self.emotionButton setImage:[UIImage imageWithName:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];[self.emotionButton setImage:[UIImage imageWithName:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];} else { // 切换为键盘按钮[self.emotionButton setImage:[UIImage imageWithName:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];[self.emotionButton setImage:[UIImage imageWithName:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];}
}/*** 添加一个按钮** @param icon 默认图标* @param highIcon 高亮图标*/
- (UIButton *)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(HMComposeToolbarButtonTypes)tag
{UIButton *button = [[UIButton alloc] init];button.tag = tag;[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];[button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];[button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];[self addSubview:button];return button;
}/*** 监听按钮点击*/
- (void)buttonClick:(UIButton *)button
{if ([self.delegate respondsToSelector:@selector(composeTool:didClickedButton:)]) {[self.delegate composeTool:self didClickedButton:button.tag];}
}// 设置按钮frame
- (void)layoutSubviews
{[super layoutSubviews];int count = self.subviews.count;CGFloat buttonW = self.width / count;CGFloat buttonH = self.height;for (int i = 0; i<count; i++) {UIButton *button = self.subviews[i];button.y = 0;button.width = buttonW;button.height = buttonH;button.x = i * buttonW;}
}@end
在控制器中
- (void)addToolBar {HMComposeToolbar *toolbar = [[HMComposeToolbar alloc]init];toolbar.width = self.view.width;toolbar.height = 44;toolbar.y = self.view.height - toolbar.height;toolbar.delegate = self;[self.view addSubview:toolbar];self.toolbar = toolbar;
}
这篇关于自定义键盘工具条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!