关键词:
代码支持:
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 ==@selector(copy:) || action ==@selector(selectAll:) || action ==@selector(cut:) || action ==@selector(select:)) BOOL isAppear = [super canPerformAction:action withSender:sender]; return isAppear; return NO; -(void)selfMenu:(id)sender self.text = [self.text stringByAppendingString:@" "]; - (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
UITextView 中的自定义自动完成
】UITextView中的自定义自动完成【英文标题】:CustomizedautocompletioninUITextView【发布时间】:2013-01-1006:11:27【问题描述】:我想为UITextView做自定义的自动补全...例如:Iftheuserstartstotype"Busines"Iwouldliketosuggest"Business1","Business2",,sothattheus... 查看详情
UITextView 的自定义委托
】UITextView的自定义委托【英文标题】:CustomDelegatefoUITextView【发布时间】:2012-06-1320:46:19【问题描述】:我是UITextView的子类,并且我得到了它的工作,但我还想在调用UITextView委托方法时做一些额外的工作。这是我到目前为止所... 查看详情
为 UITextView 旋转自定义 inputView 的问题
】为UITextView旋转自定义inputView的问题【英文标题】:ProblemwithrotatingcustominputViewforUITextView【发布时间】:2011-06-0916:24:57【问题描述】:我编写了一个自定义输入视图,用于替换UITextView的标准键盘。如果我将我的自定义UIView指定... 查看详情
未使用自定义键盘调用 UItextView 委托
】未使用自定义键盘调用UItextView委托【英文标题】:UItextViewdelegatenotcalledusingcustomkeyboard【发布时间】:2012-11-0900:13:46【问题描述】:我有几个UITextView子视图,都使用相同的自定义输入界面(基本上是带有自动填充选项和保存按... 查看详情
无法在自定义 TableViewCell 中编辑 UITextView
】无法在自定义TableViewCell中编辑UITextView【英文标题】:CannoteditUITextViewinsidecustomTableViewCell【发布时间】:2017-03-2411:48:32【问题描述】:我有一个自定义UITableViewCell和一个表格视图。UITableViewCell里面有UITextView。editable已设置为true... 查看详情
在自定义 UITableViewCell 中调整 UITextView 的大小
】在自定义UITableViewCell中调整UITextView的大小【英文标题】:ResizingUITextViewincustomUITableViewCell【发布时间】:2013-12-0908:55:33【问题描述】:我有一个自定义UITableViewCell,我正在尝试根据内容大小调整其中的UITextView。我在iOS7上并使... 查看详情
在 UITextView 上替换属性文本时保持自定义属性
】在UITextView上替换属性文本时保持自定义属性【英文标题】:MaintaincustomattributeswhenreplacingattributedtextonUITextView【发布时间】:2016-11-1821:01:28【问题描述】:我有一个可编辑的UITextView,它加载了属性字符串。此属性化字符串由应... 查看详情
iOS:通过自定义 UITableViewCell 上的 UITextView 进行选择
】iOS:通过自定义UITableViewCell上的UITextView进行选择【英文标题】:iOS:SelectingthroughUITextViewoncustomUITableViewCell【发布时间】:2010-10-2817:53:03【问题描述】:我有一个带有图像和UITextView属性的自定义UITableViewCell。textview跨越到单元格... 查看详情
如何从自定义 UITextView 单元中获取文本?
】如何从自定义UITextView单元中获取文本?【英文标题】:HowdoIgetthetextfromacustomUITextViewCell?【发布时间】:2011-08-1217:41:44【问题描述】:我正在使用包含UITextField的自定义UITableViewCell。我用这段代码在UITableView中绘制了这个UITableVie... 查看详情
SwiftUI 中自定义 UIViewRepresentable UITextView 的框架高度问题
】SwiftUI中自定义UIViewRepresentableUITextView的框架高度问题【英文标题】:FrameheightproblemwithcustomUIViewRepresentableUITextViewinSwiftUI【发布时间】:2020-02-2715:50:57【问题描述】:我正在通过UIViewRepresentable为SwiftUI构建自定义UITextView。它旨在... 查看详情
UITextView 委托行为与自定义 iOS 键盘 (SwiftKey) 不同
】UITextView委托行为与自定义iOS键盘(SwiftKey)不同【英文标题】:UITextViewDelegatesBehavingDifferentlywithCustomiOSKeyboards(SwiftKey)【发布时间】:2014-11-2908:04:10【问题描述】:使用shouldChangeTextInRange委托来检测某些短语,在输入UITextView时会在... 查看详情
带有 UITextFields 和 UITextViews 的自定义 UIAlertView
】带有UITextFields和UITextViews的自定义UIAlertView【英文标题】:customUIAlertViewwithUITextFieldsandUITextViews【发布时间】:2012-12-0213:47:28【问题描述】:我想制作一个UIAlertView,其中有一个UITextField和UITextView显示大约5-6行。我尝试创建视图... 查看详情
Objective-c中有三个UITextView时如何使用UITextView委托
】Objective-c中有三个UITextView时如何使用UITextView委托【英文标题】:HowtouseUITextViewdelegatewhentherearethreeUITextViewsinObjective-c【发布时间】:2016-06-2205:38:59【问题描述】:创建UITextViews//TextView1textview1.layer.borderWidth=1.0f;textview1.laye 查看详情
无法在自定义 UITableViewCell 中更新 UITextView 的文本
】无法在自定义UITableViewCell中更新UITextView的文本【英文标题】:Can\'tupdatetextofUITextViewinacustomUITableViewCell【发布时间】:2016-05-1217:13:23【问题描述】:我遇到了一个似乎在网络上不断出现的问题,但一直无法解决问题...我正在尝... 查看详情
如何将 uitextview 内容转换为自定义大小的图像?
】如何将uitextview内容转换为自定义大小的图像?【英文标题】:Howtoconvertuitextviewcontenttoaimageincustomsize?【发布时间】:2018-01-1209:21:28【问题描述】:我创建了一个uitextview,我可以使用NSAttributedString和NSTextAttachment向其中添加文本... 查看详情
用于点击数字的 UITextView 自定义操作
】用于点击数字的UITextView自定义操作【英文标题】:UITextViewcustomactionfortaponnumber【发布时间】:2013-09-0514:28:35【问题描述】:我的应用程序中有一个UITextView,用于显示一些带有数字的字符串。这些号码可以是电话号码或特定于... 查看详情
选择单元格内的自定义 UITextView 时选择单元格
】选择单元格内的自定义UITextView时选择单元格【英文标题】:SelectingcellwhenselectingthecustomUITextViewinsidethecell【发布时间】:2011-04-0122:25:49【问题描述】:我创建了一个自定义单元格函数,其中包含两个uitextfields,因为我不喜欢cell... 查看详情
将 UIImageView 或 UITextView 添加到自定义 UIButton 类的正确方法是啥?
】将UIImageView或UITextView添加到自定义UIButton类的正确方法是啥?【英文标题】:What\'sthecorrectwaytoaddaUIImageVieworUITextViewtoacustomUIButtonclass?将UIImageView或UITextView添加到自定义UIButton类的正确方法是什么?【发布时间】:2017-02-2108:02:47... 查看详情