iOS中的UITextView文字输入光标使用技巧小结

2025-05-29 0 104

1.创建并初始化

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
@property (nonatomic, strong) UITextView *textView;

// 创建

self.textView = [[UITextView alloc] initWithFrame:self.view.frame];

// 设置textview里面的字体颜色

self.textView.textColor = [UIColor blackColor];

// 设置字体名字和字体大小

self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];

// 设置代理

self.textView.delegate = self;

// 设置它的背景颜色

self.textView.backgroundColor = [UIColor whiteColor];

self.textView.text = @“hehe”;

// 返回键的类型

self.textView.returnKeyType = UIReturnKeyDefault;

// 键盘类型

self.textView.keyboardType = UIKeyboardTypeDefault;

// 是否可以拖动

self.textView.scrollEnabled = YES;


2. UITextView退出键盘的几种方式
(1)如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实现UITextViewDelegate。

?

1

2

3

4

5

6

7

8

9

10

11
- (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{

[self.textView resignFirstResponder];

}

(2)如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。

?

1

2

3

4

5

6

7

8

9
#pragma mark - UITextView Delegate Methods

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

if ([text isEqualToString:@"\\n"]) {

[textView resignFirstResponder];

return NO;

}

return YES;

}

(3)还有你也可以自定义其他视图控件加载到键盘上用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。

?

1

2

3

4

5

6

7

8

9
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];

[self.textView setInputAccessoryView:topView];

-(void)dismissKeyBoard

{

[tvTextView resignFirstResponder];

}

3.UITextView自定选择文字后的菜单

在ViewDidLoad中加入:

?

1

2

3

4

5

6

7

8

9

10
- (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方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。
然后还得在代码里加上一个方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
-(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的几个属性就行了

?

1

2

3

4
textView.editable = NO;//不可编辑

textView.scrollEnabled = NO;//不可滚动

textView.editable = NO;//不可编辑

textView.scrollEnabled = NO;//不可滚动

这样就ok;
但是当我们在实际运用时,想计算文字的大小并设置UITextView的显示大小

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
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是却不行,文字总是显示不全,不管你主动写多了高度给它,当文字不一样了双会显示不全或显示高度过多;
可以用下面的方法试一下

?

1

2

3

4
[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];//并设置左对齐

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS中的UITextView文字输入光标使用技巧小结 https://www.kuaiidc.com/94038.html

相关文章

发表评论
暂无评论