自定义键盘工具条

2024-04-17 08:58
文章标签 自定义 键盘 工具条

本文主要是介绍自定义键盘工具条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

这篇关于自定义键盘工具条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/911359

相关文章

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

PyQt6 键盘事件处理的实现及实例代码

《PyQt6键盘事件处理的实现及实例代码》本文主要介绍了PyQt6键盘事件处理的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录一、键盘事件处理详解1、核心事件处理器2、事件对象 QKeyEvent3、修饰键处理(1)、修饰键类

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束