当前位置:网站首页>Tips for using textviewdidchange of uitextview
Tips for using textviewdidchange of uitextview
2022-07-23 12:14:00 【Try to be a charterer】
UItextview Use in projects :
This control is generally used in projects , To achieve the following effects . Now let's realize .

stay .h In the document
{
UITextView *tvRemark; // Feedback content
UILabel *labNumber; // Number of words
}
stay .m In the document
- (void)viewDidLoad {
// Input content
UIView *viewRemark = [[UIView alloc] init];
viewRemark.backgroundColor = [UIColor whiteColor];
[self.view addSubview:viewRemark];
UILabel *labRemark = [[UILabel alloc] init];
labRemark.text = @" Feedback content ";
labRemark.font = [UIFont systemFontOfSize:15];
labRemark.textColor = [UIColor blackColor];
[viewRemark addSubview:labRemark];
UILabel *labTag = [[UILabel alloc] init];
labTag.textColor = [UIColor redColor];
labTag.text = @"*";
[viewRemark addSubview:labTag];
tvRemark = [[UITextView alloc] init];
tvRemark.font = [UIFont systemFontOfSize:13];
tvRemark.delegate = self;
[viewRemark addSubview:tvRemark];
// _placeholderLabel
UILabel *placeHolderLabel = [[UILabel alloc] init];
placeHolderLabel.text = @" Please fill in 10 More than words of problem description , So that we can better help you solve the problem ";
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.textColor = [UIColor lightGrayColor];
[placeHolderLabel sizeToFit];
placeHolderLabel.font = [UIFont systemFontOfSize:13.f];
[tvRemark addSubview:placeHolderLabel];
[tvRemark setValue:placeHolderLabel forKey:@"_placeholderLabel"];
labNumber = [[UILabel alloc] init];
labNumber.textColor = [UIColor lightTextColor];
labNumber.font = [UIFont systemFontOfSize:13];
labNumber.text = @"0/200";
[viewRemark addSubview:labNumber];
// What is used below is Masonry A third party SDK The layout of the controls
[viewRemark mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(viewPic.mas_bottom).offset(10);
make.height.mas_equalTo(168);
make.width.mas_equalTo(SCREEN_WIDTH);
}];
[labRemark mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(15);
make.top.mas_equalTo(viewRemark.mas_top).offset(8);
make.height.mas_equalTo(20);
}];
[labTag mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(labRemark.mas_right).offset(5);
make.top.bottom.mas_equalTo(labRemark);
}];
[tvRemark mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.top.mas_equalTo(labRemark.mas_bottom).offset(8);
make.height.mas_equalTo(100);
}];
[labNumber mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-15);
make.top.mas_equalTo(tvRemark.mas_bottom).offset(8);
make.height.mas_equalTo(15);
make.bottom.mas_equalTo(viewRemark.mas_bottom).offset(-20);
}];
}
The following code is more important , The method is textview Of UITextViewDelegate. Is to control the change of the number of input words .
stay .m In the document
-(void)textViewDidChange:(UITextView *)textView{
NSString *lang = textView.textInputMode.primaryLanguage; // Keyboard input mode
if ([lang isEqualToString:@"zh-Hans"]){
UITextRange *selectedRange = [textView markedTextRange];
if (!selectedRange) { // No highlight
if (textView.text.length>200) { // The input text is longer than the set length The setting here is 200 Characters
tvRemark.text = [textView.text substringToIndex:200];
}else{
tvRemark.text = textView.text;
}
labNumber.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)textView.text.length];
}else{
}
}else{
if (textView.text.length>200) {
tvRemark.text = [textView.text substringToIndex:200];
}else{
tvRemark.text = textView.text;
}
labNumber.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)textView.text.length];
}
}
tvRemark Is initialized UItextview; labNumber Is to display the input text UIlabel Control . among
[lang isEqualToString:@"zh-Hans"] This sentence is to judge whether it is Chinese , Prevent English synchronization from being counted .
selectedRange Judge TextView Whether it is in the highlighted state .
textView.text.length Indicates the number of text currently entered .
In later use , Colleagues found problems . Use the above -(void)textViewDidChange:(UITextView *)textView There will be a problem of jumping directly to the end after entering a sentence in the middle . So it was modified . as follows :
-(void)textViewDidChange:(UITextView *)textView{
NSString *lang = textView.textInputMode.primaryLanguage;// Keyboard input mode
if ([lang isEqualToString:@"zh-Hans"]){
UITextRange *selectedRange = [textView markedTextRange];
if (!selectedRange) {
// Marked text
if (textView.text.length>200) {
self.tvRemark.text = [textView.text substringToIndex:200];
}
}
}else{
if (textView.text.length>200) {
self.tvRemark.text = [textView.text substringToIndex:200];
}
}
self.labNumber.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)self.tvRemark.text.length];
if (_delegate && [_delegate respondsToSelector:@selector(CtextViewTextChange:)]) {
[self.delegate CtextViewTextChange:self.tvRemark.text];
}
}
Through the above code, we can realize UItextview Common page status . If the above code is used in multiple places , It is recommended to create a new class , Manage the code in a unified way .
边栏推荐
- 2021可信隐私计算高峰论坛暨数据安全产业峰会上百家争鸣
- 生命科学领域下的医药研发通过什么技术?冷冻电镜?分子模拟?IND?
- Service Service
- 常用数学知识汇总
- What is the IP address
- 论文解读:《开发一种基于多层深度学习的预测模型来鉴定DNA N4-甲基胞嘧啶修饰》
- The green data center "counting from the east to the west" was fully launched
- The use of padding.nn.bceloss
- 建设“绿色计算”,解读“智算中心”
- The data set needed to generate yolov3 from the existing voc207 data set, and the places that need to be modified to officially start the debugging program
猜你喜欢

深度学习-神经网络

Pytorch个人记录(请勿打开)

Vio --- boundary adjustment solution process

After the VR project of ue4.24 is packaged, the handle controller does not appear

Notes | Baidu flying plasma AI talent Creation Camp: How did amazing ideas come into being?

2021信息科学Top10发展态势。深度学习?卷积神经网络?

以不太严谨但是有逻辑的数学理论---剖析VIO之预积分

Gaode positioning - the problem that the permission pop-up box does not appear

利用or-tools来求解路径规划问题(VRP)

笔记 | 百度飞浆AI达人创造营:数据获取与处理(以CV任务为主)
随机推荐
利用or-tools来求解路径规划问题(TSP)
NLP自然语言处理-机器学习和自然语言处理介绍(二)
论文解读:《BERT4Bitter:一种基于transformer(BERT)双向编码器表示用于改善苦肽预测的基础模型》
笔记 | 百度飞浆AI达人创造营:深度学习模型训练和关键参数调优详解
抽象类和接口有什么区别?
笔记 | 百度飞浆AI达人创造营:数据获取与处理(以CV任务为主)
How to build a liquid cooling data center is supported by blue ocean brain liquid cooling technology
ADB common commands
智能指针shared_ptr和unique_ptr
绿色数据中心:风冷GPU服务器和水冷GPU服务器综合分析
数据分析的重要性
Service Service
Practical convolution correlation trick
All kinds of ice! Use paddegan of the propeller to realize makeup migration
Hard disk partition of obsessive-compulsive disorder
可能逃不了课了!如何使用paddleX来点人头?
How to develop the computing power and AI intelligent chips in the data center of "digital computing in the East and digital computing in the west"?
BST tree
Chain queue
论文解读:《Deep-4mcw2v: 基于序列的预测器用于识别大肠桿菌中的 N4- 甲基胞嘧啶(4mC)位点》