UITextField篇
2020-04-22 本文已影响0人
时无寻
监听文字改变通知
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: .UITextFieldTextDidChange, object: nil)
@objc func textDidChange(_ notification: Notification) {
}
键盘添加完成按钮及事件
swift
public extension UITextField {
static var doneKey = "doneKey"
var doneClosure: (()->Void)? {
set {
objc_setAssociatedObject(
self,
&UITextField.doneKey,
newValue,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
get {
return objc_getAssociatedObject(self, &UITextField.doneKey) as? ()->Void
}
}
func showDoneButton(callback:(()->Void)?) {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: Screen.width, height: 35.0))
toolbar.tintColor = UIColor.systemBlue
toolbar.backgroundColor = UIColor(red: 230.0/255.0, green: 230.0/255.0, blue: 230.0/255.0, alpha: 1)
let flexibleSpaceBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
let doneBtn = UIBarButtonItem(title: "完成", style: UIBarButtonItem.Style.plain, target: self, action: #selector(doneAction))
toolbar.items = [flexibleSpaceBtn, doneBtn]
self.inputAccessoryView = toolbar
doneClosure = callback
}
@objc func doneAction() {
self.resignFirstResponder()
doneClosure?()
}
}
oc
.h文件
#import <UIKit/UIKit.h>
@interface UITextField (XXExtension)
/// 键盘上方显示"完成“按钮
/// @param callback 回调
- (void)xx_showDoneButton:(void(^)(void))callback;
@end
.m文件
#import "UITextField+XXExtension.h"
#import "XXMacros.h"
#import <objc/runtime.h>
@interface UITextField ()
@property (nonatomic, copy) void(^doneBlock)(void);
@end
@implementation UITextField (XXExtension)
#pragma mark -
- (void)xx_showDoneButton:(void(^)(void))callback {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 35)];
toolbar.tintColor = UIColor.systemBlueColor;
toolbar.backgroundColor = [[UIColor alloc] initWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
UIBarButtonItem *flexibleSpaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(xx_doneAction)];
toolbar.items = @[flexibleSpaceBtn, doneBtn];
self.inputAccessoryView = toolbar;
self.doneBlock = callback;
}
- (void)xx_doneAction {
if (self.doneBlock) {
self.doneBlock();
}
}
- (void)setDoneBlock:(void (^)(void))doneBlock {
objc_setAssociatedObject(self, @selector(doneBlock), doneBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(void))doneBlock {
return objc_getAssociatedObject(self, @selector(doneBlock));
}
@end
设置placeholder样式
public extension UITextField {
func setupPlaceholder(placeholder: String?, color: UIColor, font: UIFont) {
if placeholder == nil { return }
let attText = NSMutableAttributedString(string: placeholder!)
attText.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: NSRange(location: 0, length: placeholder!.count))
attText.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: placeholder!.count))
attributedPlaceholder = attText
}
}