如圖所示:這是一個字符串,在那些橫線的位置我需要添加文本框來供填寫。幾經周折冒签,我用了一個比較古老的方法把這個問題解決了控汉。
貼上代碼看看吧:
[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];
}
//創(chuàng)建文本框
-?(void)forCreateText
{
//1.根據“\n”把整個字符串分割存放在數組中
NSArray*arrayNo1=[_strQuestioncomponentsSeparatedByString:@"\n"];//?這里的_strQuestion就是一個字符串
NSMutableArray*arrayQuestionNo1=[NSMutableArrayarrayWithArray:arrayNo1];
//創(chuàng)建變量,計算整個句子的高度
CGSize?detailSize?=?[@"the"sizeWithFont:[UIFontsystemFontOfSize:19]constrainedToSize:CGSizeMake(MAXFLOAT,?MAXFLOAT)lineBreakMode:UILineBreakModeWordWrap];
//創(chuàng)建變量姑子,計算“_________”的高寬度
CGSize?lineSize?=?[LINEsizeWithFont:[UIFontsystemFontOfSize:19]constrainedToSize:CGSizeMake(MAXFLOAT,?MAXFLOAT)lineBreakMode:UILineBreakModeWordWrap];
//循環(huán)取出根據“\n”分割成的數組中的數據
for(inti=0;?i
_questionString=[arrayQuestionNo1objectAtIndex:i];
//判斷是否含有“__________”
if([_questionStringrangeOfString:@"____________"].location!=NSNotFound)?{
//2.根據“__________”把字符串分割存儲在數組中
NSArray*array=[_questionStringcomponentsSeparatedByString:@"____________"];
arrayQuestion=[NSMutableArrayarrayWithArray:array];
//????????NSLog(@"題目分割為:%@",arrayQuestion);
//3.判斷元素個數乎婿,然后在相應位置后面追加上“__________”(便于計算創(chuàng)建文本框的位置)
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個元素變?yōu)榍?個元素加上原本的第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個元素變?yōu)榍?個元素加上原本的第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個元素變?yōu)榍?個元素加上原本的第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個元素變?yōu)榍?個元素加上原本的第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個元素變?yōu)榍?個元素加上原本的第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.創(chuàng)建文本框
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];//類型轉換
//在這里創(chuàng)建文本框
_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