【Objective-C】自定义UITextView(placeholder,长按换行,文字位置、文字间距等)
代码支持:
1、长按textView弹出换行操作;
2、自定义文字间距;
3、为textView添加placeholder文字;
直接贴代码:
1、.m文件
#import "TextView.h" @interface TextView () { NSInteger b_index; } @end @implementation TextView CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25; - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; #if __has_feature(objc_arc) #else [_placeHolderLabel release]; _placeHolderLabel = nil; [_placeholderColor release]; _placeholderColor = nil; [_placeholder release]; _placeholder = nil; [super dealloc]; #endif } - (void)awakeFromNib { [super awakeFromNib]; if (!self.placeholder) { _placeholder = @""; } if (!self.placeholderColor) { [self setPlaceholderColor:[UIColor lightGrayColor]]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } - (id)initWithFrame:(CGRect)frame { if( (self = [super initWithFrame:frame]) ) { _placeholder = @""; self.tintColor = [UIColor colorWithHexString:@"#B1B5BA"]; self.returnKeyType = UIReturnKeyDone; [self setPlaceholderColor:[UIColor lightGrayColor]]; //设置菜单 UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"换行" action:@selector(selfMenu:)]; UIMenuController *menuController = [UIMenuController sharedMenuController]; [menuController setMenuItems:[NSArray arrayWithObject:menuItem]]; [menuController setMenuVisible:NO]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } return self; } -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{ if (action == @selector(selfMenu:)) { return YES; }else if(action (copy:) || action (selectAll:) || action (cut:) || action ==@selector(select:)){ BOOL isAppear = [super canPerformAction:action withSender:sender]; return isAppear; } return NO; } -(void)selfMenu:(id)sender{ self.text = [self.text stringByAppendingString:@"\n"]; } - (void)textChanged:(NSNotification *)notification { if([[self placeholder] length] == 0) { return; } [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{ if([[self text] length] == 0) { [[self viewWithTag:999] setAlpha:1]; } else { [[self viewWithTag:999] setAlpha:0]; } }]; } - (void)setText:(NSString *)text { [super setText:text]; [self textChanged:nil]; } - (void)drawRect:(CGRect)rect { if( [[self placeholder] length] > 0 ) { UIEdgeInsets insets = self.textContainerInset; if (_placeHolderLabel == nil ) { _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(insets.left+10,5,self.bounds.size.width - (insets.left +insets.right+20),1.0)];//insets.top _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; _placeHolderLabel.font = self.font; _placeHolderLabel.backgroundColor = [UIColor clearColor]; _placeHolderLabel.textColor = self.placeholderColor; _placeHolderLabel.alpha = 0; _placeHolderLabel.tag = 999; [self addSubview:_placeHolderLabel]; } _placeHolderLabel.text = self.placeholder; [_placeHolderLabel sizeToFit];//insets.top [_placeHolderLabel setFrame:CGRectMake(insets.left+10,5,self.bounds.size.width - (insets.left +insets.right),CGRectGetHeight(_placeHolderLabel.frame))]; [self sendSubviewToBack:_placeHolderLabel]; } if( [[self text] length] == 0 && [[self placeholder] length] > 0 ) { [[self viewWithTag:999] setAlpha:1]; } [super drawRect:rect]; } - (void)setPlaceholder:(NSString *)placeholder{ if (_placeholder != placeholder) { _placeholder = placeholder; [self setNeedsDisplay]; } } -(void)textViewlineSpacing:(TextView *)textView{ if (textView.text.length < 1) { textView.text = @"间距"; } NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 4;// 字体的行间距 NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:13], NSParagraphStyleAttributeName:paragraphStyle }; textView.attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attributes]; textView.textColor = kTextColor_Black; if ([textView.text isEqualToString:@"间距"]) { textView.attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:attributes]; } } @end
2、.h文件
#import <UIKit/UIKit.h> @interface TextView : UITextView @property (nonatomic, strong) NSString *placeholder; @property (nonatomic, strong) UIColor *placeholderColor; @property (nonatomic, strong) UILabel *placeHolderLabel; -(void)textChanged:(NSNotification*)notification; -(void)textViewlineSpacing:(TextView *)textView; @end
相关推荐
JM 2020-05-04
haidaoxianzi 2020-04-18
攻城师 2013-05-14
haohong 2020-01-17
JM 2019-12-22
cherry0 2019-12-13
拭血 2019-11-17
hnyzyty 2019-11-04
zyhui 2019-09-08
wcssdu 2017-11-01
jiedinghui 2018-04-03
zZoOoOJaVa 2018-02-19
MayMatrix 2015-01-09
hualala 2019-07-01
LiteHeaven 2019-06-30
mjbaishiyun 2019-06-30