iOS圖文混排(需要在文本或者字符串中某些特定位置添加文本框)

在做項目過程中,我遇到這樣一個坎:



如圖所示:這是一個字符串,在那些橫線的位置我需要添加文本框來供填寫。幾經周折,我用了一個比較古老的方法把這個問題解決了。

貼上代碼看看吧:

//
//  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
這種方法固然是解決了問題,但是我知道一定有更完美的方法,只是我還不會,大神有好的方法,洗耳恭聽,還請賜教。好噠,繼續工作繼續學習吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章