如图所示:这是一个字符串,在那些横线的位置我需要添加文本框来供填写。几经周折,我用了一个比较古老的方法把这个问题解决了。
贴上代码看看吧:
[objc]view plaincopy
//
// Layout5Controller.m
// happylearning
//
// Created by WuShuliang on 16/3/25.
// Copyright © 2016年 ajing. All rights reserved.
//
#import "Layout5Controller.h"
#import "ContentController.h"
#import
#define LINE @"____________"
@interfaceLayout5Controller ()
{
NSString*_questionString;//"\n"分割后数组中的元素
NSString*questionString;//LINE分割后数组中的元素
NSMutableArray*arrayQuestion;//根据LINE把字符串分割存储在数组中
NSMutableArray*_arrayQuestionWidth;//"\n"分割后数组中的元素存进去
NSMutableArray*_arrayQuestionHeight;//"\n"分割后数组中的行数存进去
}
@end
@implementationLayout5Controller
- (void)viewDidLoad {
[superviewDidLoad];
_txtAnswer.delegate=self;
_questionString=[[NSStringalloc]init];
questionString=[[NSStringalloc]init];
arrayQuestion=[[NSMutableArrayalloc]initWithCapacity:0];
_arrayQuestionWidth=[[NSMutableArrayalloc]initWithCapacity:0];
_arrayQuestionHeight=[[NSMutableArrayalloc]initWithCapacity:0];
}
- (void)viewDidAppear:(BOOL)animated
{
// _lblAnswer1.lineBreakMode = UILineBreakModeWordWrap;//换行模式。
[selfforCreateText];
}
//创建文本框
- (void)forCreateText
{
//1.根据“\n”把整个字符串分割存放在数组中
NSArray*arrayNo1=[_strQuestioncomponentsSeparatedByString:@"\n"];// 这里的_strQuestion就是一个字符串
NSMutableArray*arrayQuestionNo1=[NSMutableArrayarrayWithArray:arrayNo1];
//创建变量,计算整个句子的高度
CGSize detailSize = [@"the"sizeWithFont:[UIFontsystemFontOfSize:19]constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)lineBreakMode:UILineBreakModeWordWrap];
//创建变量,计算“_________”的高宽度
CGSize lineSize = [LINEsizeWithFont:[UIFontsystemFontOfSize:19]constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)lineBreakMode:UILineBreakModeWordWrap];
//循环取出根据“\n”分割成的数组中的数据
for(inti=0; i
_questionString=[arrayQuestionNo1objectAtIndex:i];
//判断是否含有“__________”
if([_questionStringrangeOfString:@"____________"].location!=NSNotFound) {
//2.根据“__________”把字符串分割存储在数组中
NSArray*array=[_questionStringcomponentsSeparatedByString:@"____________"];
arrayQuestion=[NSMutableArrayarrayWithArray:array];
// NSLog(@"题目分割为:%@",arrayQuestion);
//3.判断元素个数,然后在相应位置后面追加上“__________”(便于计算创建文本框的位置)
if(arrayQuestion.count>2) {//有两个以上元素
// NSLog(@"多根“__________”");
for(intj=0; j
if(j==0) {
//1.被“__________”分割的组成的数组的第1个元素
questionString=arrayQuestion[0];
//2.在第1个元素后边加上“__________”
arrayQuestion[0]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
if(j==1) {
//1.被“__________”分割的组成的数组的第2个元素
questionString=[NSStringstringWithFormat:@"%@%@",arrayQuestion[0],arrayQuestion[1]];
//2.第2个元素变为前1个元素加上原本的第2个元素后边加上“__________”
arrayQuestion[1]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];//
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
if(j==2) {
//1.被“__________”分割的组成的数组的第3个元素
questionString=[NSStringstringWithFormat:@"%@%@",arrayQuestion[1],arrayQuestion[2]];
//2.第3个元素变为前2个元素加上原本的第3个元素后边加上“__________”
arrayQuestion[2]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
if(j==3) {
//1.被“__________”分割的组成的数组的第4个元素
questionString=[NSStringstringWithFormat:@"%@%@",arrayQuestion[2],arrayQuestion[3]];
//2.第4个元素变为前3个元素加上原本的第4个元素后边加上“__________”
arrayQuestion[3]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
if(j==4) {
//1.被“__________”分割的组成的数组的第5个元素
questionString=[NSStringstringWithFormat:@"%@%@",arrayQuestion[3],arrayQuestion[4]];
//2.第5个元素变为前4个元素加上原本的第5个元素后边加上“__________”
arrayQuestion[4]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
if(j==5) {
//1.被“__________”分割的组成的数组的第6个元素
questionString=[NSStringstringWithFormat:@"%@%@",arrayQuestion[4],arrayQuestion[5]];
//2.第6个元素变为前5个元素加上原本的第6个元素后边加上“__________”
arrayQuestion[5]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
}
}
//数组的第一个元素后边加上1根横线
else//两个和两个以下元素
{
//1.被“__________”分割的组成的数组的第一个元素
questionString=arrayQuestion[0];
//2.在第1个元素后边加上“__________”
arrayQuestion[0]=[NSStringstringWithFormat:@"%@%@",questionString,LINE];
//3.把元素添加到数组中,用来计算放置文本框的位置
[_arrayQuestionWidthaddObject:questionString];
[_arrayQuestionHeightaddObject:[NSStringstringWithFormat:@"%i",i]];
}
}
// NSLog(@"array:%@\n%@",_arrayQuestionWidth,_arrayQuestionHeight);
}
//4.创建文本框
for(inti=0; i<_arrayQuestionWidth.count; i++) {
//计算文本框之前的长度(x坐标)
CGSize headLength=[[_arrayQuestionWidthobjectAtIndex:i]sizeWithFont:[UIFontsystemFontOfSize:19]constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)lineBreakMode:UILineBreakModeWordWrap];
//第几行
NSString*strLine=[_arrayQuestionHeightobjectAtIndex:i];
intintLine=[strLineintValue];//类型转换
//在这里创建文本框
_txtAnswer=[[UITextFieldalloc]initWithFrame:CGRectMake(headLength.width, detailSize.height*intLine,lineSize.width, detailSize.height)];
_txtAnswer.font= [UIFontfontWithName:@"Arial"size:19.0f];
// _txtAnswer.backgroundColor=[UIColor redColor];
_txtAnswer.textColor= [UIColorblackColor];
_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.lblAnswer1addSubview:_txtAnswer];
}
}
#pragma mark - 限制文字长度
-(void)LimitTextFieldLength:(id)sender{
if(_txtAnswer.text.length>25) {
_txtAnswer.text= [_txtAnswer.textsubstringToIndex:25];
}
}
// 键盘失去第一响应者
-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{
[self.viewendEditing:NO];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end