iOS图文混排(需要在文本或者字符串中某些特定位置添加文本框)
2016-05-04 10:18
537 查看
在做项目过程中,我遇到这样一个坎:
如图所示:这是一个字符串,在那些横线的位置我需要添加文本框来供填写。几经周折,我用了一个比较古老的方法把这个问题解决了。
贴上代码看看吧:
如图所示:这是一个字符串,在那些横线的位置我需要添加文本框来供填写。几经周折,我用了一个比较古老的方法把这个问题解决了。
贴上代码看看吧:
// // Layout5Controller.m // happylearning // // Created by WuShuliang on 16/3/25. // Copyright © 2016年 ajing. All rights reserved. // #import "Layout5Controller.h" #import "ContentController.h" #import <CoreText/CoreText.h> #define LINE @"____________" @interface Layout5Controller ()<UITextFieldDelegate> { NSString *_questionString;//"\n"分割后数组中的元素 NSString *questionString;//LINE分割后数组中的元素 NSMutableArray *arrayQuestion;//根据LINE把字符串分割存储在数组中 NSMutableArray *_arrayQuestionWidth;//"\n"分割后数组中的元素存进去 NSMutableArray *_arrayQuestionHeight;//"\n"分割后数组中的行数存进去 } @end @implementation Layout5Controller - (void)viewDidLoad { [super viewDidLoad]; _txtAnswer.delegate=self; _questionString=[[NSString alloc]init]; questionString=[[NSString alloc]init]; arrayQuestion=[[NSMutableArray alloc]initWithCapacity:0]; _arrayQuestionWidth=[[NSMutableArray alloc]initWithCapacity:0]; _arrayQuestionHeight=[[NSMutableArray alloc]initWithCapacity:0]; } - (void)viewDidAppear:(BOOL)animated { // _lblAnswer1.lineBreakMode = UILineBreakModeWordWrap;//换行模式。 [self forCreateText]; } //创建文本框 - (void)forCreateText { //1.根据“\n”把整个字符串分割存放在数组中 NSArray *arrayNo1=[_strQuestion componentsSeparatedByString:@"\n"];// 这里的_strQuestion就是一个字符串 NSMutableArray *arrayQuestionNo1=[NSMutableArray arrayWithArray:arrayNo1]; //创建变量,计算整个句子的高度 CGSize detailSize = [@"the" sizeWithFont:[UIFont systemFontOfSize:19] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; //创建变量,计算“_________”的高宽度 CGSize lineSize = [LINE sizeWithFont:[UIFont systemFontOfSize:19] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; //循环取出根据“\n”分割成的数组中的数据 for (int i=0; i<arrayQuestionNo1.count; i++) { _questionString=[arrayQuestionNo1 objectAtIndex:i]; //判断是否含有“__________” if ([_questionString rangeOfString:@"____________"].location !=NSNotFound) { //2.根据“__________”把字符串分割存储在数组中 NSArray *array=[_questionString componentsSeparatedByString:@"____________"]; arrayQuestion=[NSMutableArray arrayWithArray:array]; // NSLog(@"题目分割为:%@",arrayQuestion); //3.判断元素个数,然后在相应位置后面追加上“__________”(便于计算创建文本框的位置) if (arrayQuestion.count>2) {//有两个以上元素 // NSLog(@"多根“__________”"); for (int j=0; j<arrayQuestion.count-1; j++) { if (j==0) { //1.被“__________”分割的组成的数组的第1个元素 questionString=arrayQuestion[0]; //2.在第1个元素后边加上“__________” arrayQuestion[0]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString]; [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } if (j==1) { //1.被“__________”分割的组成的数组的第2个元素 questionString=[NSString stringWithFormat:@"%@%@",arrayQuestion[0],arrayQuestion[1]]; //2.第2个元素变为前1个元素加上原本的第2个元素后边加上“__________” arrayQuestion[1]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString];// [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } if (j==2) { //1.被“__________”分割的组成的数组的第3个元素 questionString=[NSString stringWithFormat:@"%@%@",arrayQuestion[1],arrayQuestion[2]]; //2.第3个元素变为前2个元素加上原本的第3个元素后边加上“__________” arrayQuestion[2]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString]; [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } if (j==3) { //1.被“__________”分割的组成的数组的第4个元素 questionString=[NSString stringWithFormat:@"%@%@",arrayQuestion[2],arrayQuestion[3]]; //2.第4个元素变为前3个元素加上原本的第4个元素后边加上“__________” arrayQuestion[3]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString]; [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } if (j==4) { //1.被“__________”分割的组成的数组的第5个元素 questionString=[NSString stringWithFormat:@"%@%@",arrayQuestion[3],arrayQuestion[4]]; //2.第5个元素变为前4个元素加上原本的第5个元素后边加上“__________” arrayQuestion[4]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString]; [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } if (j==5) { //1.被“__________”分割的组成的数组的第6个元素 questionString=[NSString stringWithFormat:@"%@%@",arrayQuestion[4],arrayQuestion[5]]; //2.第6个元素变为前5个元素加上原本的第6个元素后边加上“__________” arrayQuestion[5]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString]; [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } } } //数组的第一个元素后边加上1根横线 else//两个和两个以下元素 { //1.被“__________”分割的组成的数组的第一个元素 questionString=arrayQuestion[0]; //2.在第1个元素后边加上“__________” arrayQuestion[0]=[NSString stringWithFormat:@"%@%@",questionString,LINE]; //3.把元素添加到数组中,用来计算放置文本框的位置 [_arrayQuestionWidth addObject:questionString]; [_arrayQuestionHeight addObject:[NSString stringWithFormat:@"%i",i]]; } } // NSLog(@"array:%@\n%@",_arrayQuestionWidth,_arrayQuestionHeight); } //4.创建文本框 for (int i=0; i<_arrayQuestionWidth.count; i++) { //计算文本框之前的长度(x坐标) CGSize headLength=[[_arrayQuestionWidth objectAtIndex:i] sizeWithFont:[UIFont systemFontOfSize:19] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; //第几行 NSString *strLine=[_arrayQuestionHeight objectAtIndex:i]; int intLine=[strLine intValue];//类型转换 //在这里创建文本框 _txtAnswer=[[UITextField alloc]initWithFrame:CGRectMake(headLength.width, detailSize.height*intLine,lineSize.width, detailSize.height)]; _txtAnswer.font = [UIFont fontWithName:@"Arial" size:19.0f]; // _txtAnswer.backgroundColor=[UIColor redColor]; _txtAnswer.textColor = [UIColor blackColor]; _txtAnswer.textAlignment = UITextAlignmentCenter; //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 _txtAnswer.adjustsFontSizeToFitWidth = YES; //设置自动缩小显示的最小字体大小 _txtAnswer.minimumFontSize = 19; //限制文本框输入长度(如果需要的话,就把一下方法加上) // [self.txtAnswer addTarget:self action:@selector(LimitTextFieldLength:) forControlEvents:UIControlEventEditingChanged]; //设置键盘的样式 _txtAnswer.keyboardType = UIKeyboardTypeDefault; _txtAnswer.borderStyle=UITextBorderStyleNone; //允许用户交互 self.lblAnswer1.userInteractionEnabled = true; self.txtAnswer.userInteractionEnabled = true; [self.lblAnswer1 addSubview:_txtAnswer]; } } #pragma mark - 限制文字长度 -(void) LimitTextFieldLength:(id)sender{ if (_txtAnswer.text.length>25) { _txtAnswer.text = [_txtAnswer.text substringToIndex:25]; } } // 键盘失去第一响应者 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.view endEditing:NO]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end这种方法固然是解决了问题,但是我知道一定有更完美的方法,只是我还不会,大神有好的方法,洗耳恭听,还请赐教。好哒,继续工作继续学习吧。
相关文章推荐
- IOS深入理解GCD之dispatch_sync
- iOS——百度统计
- iOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- ios 支付宝:移动支付2
- xamarin 学习笔记02- IOS Simulator for windows 安装
- iOS开发 ☞ NSNotification
- iOS AVFoundation实现相机功能
- iOS 随机数的使用
- iOS 短信验证码倒计时按钮的实现
- IOS9 适配
- 斯坦福大学 iOS 开发公开课总结
- iOS开发简单高效的数据存储
- iOS属性声明strong和retain竟然不一样
- iOS开发实用技术_Touch ID
- iOS开发之安全学习资料汇总,史上最全安全学习资料
- Github上的iOS App源码 (中文)
- iOS 钥匙串 保存登录个人信息
- iOS应用开发中使用NSLocale类实现对象信息的本地化
- WiFi-Portal认证中的坑:iOS9.3.1弹出portal慢+认证失败问题
- 资料