iOS中的UITextView⽂字输⼊光标使⽤技巧⼩结
1.创建并初始化
@property (nonatomic, strong) UITextView *textView;
// 创建
// 设置textview⾥⾯的字体颜⾊
// 设置字体名字和字体⼤⼩
// 设置代理
/
/ 设置它的背景颜⾊
< = @“hehe”;
// 返回键的类型
// 键盘类型
// 是否可以拖动
2. UITextView退出键盘的⼏种⽅式
(1)如果你程序是有导航条的,可以在导航条上⾯加多⼀个Done的按钮,⽤来退出键盘,当然要先实现UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(getOverEditing)]; }
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)getOverEditing{
[View resignFirstResponder];
}
(2)如果你的textview⾥不⽤回车键,可以把回车键当做退出键盘的响应键。
#pragma mark - UITextView Delegate Methods
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];view ui框架
return NO;
}
return YES;
}
(3)还有你也可以⾃定义其他视图控件加载到键盘上⽤来退出,⽐如在弹出的键盘上⾯加⼀个view来放置退出键盘的Done按钮。
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem * cancelButton= [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = @[cancelButton];
[topView setItems:buttonsArray];
[View setInputAccessoryView:topView];
-(void)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}
3.UITextView⾃定选择⽂字后的菜单
在ViewDidLoad中加⼊:
- (void)viewDidLoad
{
[super viewDidLoad];
self._textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];
[self.view addSubview:_textView];
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@“我是⾃定义的菜单" action:@selector(didClickCustomMenuAction)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]];
[menuItem release];
}
当然上⾯那个@selector⾥⾯的changeColor⽅法还是⾃⼰写吧,也就是说点击了我们⾃定义的菜单项后会触发的⽅法。
然后还得在代码⾥加上⼀个⽅法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(changeColor) || action == @selector(copy:))
{
if(_textView.selectedRange.length>0)
return YES;
}
return NO;
}
-(void)didClickCustomMenuAction
{
NSLog(@"%@“,__function__);
}
4.设置UITextView内边距
当我们因为⼀些需求将UITextView当成UILabel使⽤(为了使⽤UITextView⾃带的复制,粘贴,选择功能),这时我们只需要禁⽤UITextView 的⼏个属性就⾏了
textView.editable = NO;//不可编辑
textView.scrollEnabled = NO;//不可滚动
textView.editable = NO;//不可编辑
textView.scrollEnabled = NO;//不可滚动
这样就ok;
但是当我们在实际运⽤时,想计算⽂字的⼤⼩并设置UITextView的显⽰⼤⼩
UIFont *font = [UIFont systemFontOfSize:14.0f]; //指定字符串的⼤⼩
[textView setText:content];
CGSize textSize = [content sizeWithFont:font constrainedToSize:CGSizeMake(200, 2000) lineBreakMode:UILineBreakModeCharacterWrap];
CGRect articleframe = [articleLabel frame];
textView.size.height = textSize.height ;
textView.size.width = textSize.width;
[textView setFrame:articleframe];
UIFont *font = [UIFont systemFontOfSize:14.0f]; //指定字符串的⼤⼩
[textView setText:content];
CGSize textSize = [content sizeWithFont:font constrainedToSize:CGSizeMake(200, 2000) lineBreakMode:UILineBreakModeCharacterWrap];
CGRect articleframe = [articleLabel frame];
textView.size.height = textSize.height ;
textView.size.width = textSize.width;
[textView setFrame:articleframe];
但是通过这种⽅法在UILabel上使⽤没有任何问题,但是在UITextView是却不⾏,⽂字总是显⽰不全,不管你主动写多了⾼度给它,当⽂字不⼀样了双会显⽰不全或显⽰⾼度过多;
可以⽤下⾯的⽅法试⼀下
[self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//设置UITextView的内边距
[self.articleLabel setTextAlignment:NSTextAlignmentLeft];//并设置左对齐
[self.articleLabel setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//设置UITextView的内边距
[self.articleLabel setTextAlignment:NSTextAlignmentLeft];//并设置左对齐
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论