2014-12-25 3 views
1

(이 질문은 내 다른 질문과 중복되는 것으로 알고 있지만 이는 보류 상태였습니다. 질문을 다시 작성했을 때 새로운 질문으로 게시하도록 지시받지 못했습니다. 거기에 대한 답변이 내 요청을 해결할 수 있습니다.)텍스트 표시 iOS의 단어 단위로 단어

나는 매우 쉽게 달성해야한다고 생각하는 요청이 있지만 어디서나 솔루션을 찾을 수 없습니다.

약 1 단 정도의 텍스트가있는 문자열이 있습니다. 그 텍스트를 UITextView에 표시하려고하지만 특별한 방법으로 표시하려고합니다. 저는 오래된 컴퓨터 단말기에서 영화에서 보는 것과 같은 종류의 단어를 하나씩 인쇄하고 싶습니다.

문자열을 분리하는 데 문제가 없지만 문제는 개인 텍스트를 개별적으로 인쇄 할 수없는 것입니다. UItextView는 self.textView라고합니다. 내 코드는 다음과 같습니다 :

for (int wordIndex = 0; wordIndex < [wordList count]; wordIndex++) { 
    //[self.textView setText: [NSString stringWithFormat:@"%@ %@",self.textView.text, [wordList objectAtIndex:wordIndex]]]; 
NSString *newText = [NSString stringWithFormat:@"%@", [wordList objectAtIndex:wordIndex]]; 

fullString = [NSString stringWithFormat:@"%@ %@",fullString, newText]; 

[self.textView performSelector:@selector(setText:) withObject:fullString afterDelay:.9]; 

}  

여기서 fullString은 전역 변수입니다. Wordlist NSArray 내 단어 (문자열로)입니다.

추가 설명이 필요하면 언제든지 물어보십시오. 정말 고마워!

+0

NSArray * yourWordList = [yourstring componentsSeparatedByString : @ ""]; –

답변

1

@ 필립 밀스와 @Ashish Kakkad의 답변을 사용하여 이것을 알아 냈습니다. 여기 내 코드입니다 (나는 누군가가 나를 위해 그것을 편집 할 수 있다면 그 좋지 않을까, 그래서 제대로 형식을 얻을 수 없습니다) : 나는 애니메이션을 발사 할 때마다

- (void) startTimer { 
    if (self.wordtimer == nil) { 
     self.wordtimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 
                target: self 
               selector: @selector(printWords) 
               userInfo: nil 
                repeats: YES]; 
    } 
} 

- (void) stopTimer { 
    if (self.wordtimer || [self.wordtimer isValid]) 
    { 
     [self.wordtimer invalidate]; 
     self.wordtimer = nil; 
    } 
} 

-(void)printWords { 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

Element *tempElement = [[Element alloc] init]; 
tempElement = [self.elementsArray objectAtIndex:appDelegate.currentElement]; 

NSArray *wordList = [tempElement.text componentsSeparatedByString:@" "]; 

if(wordIndex < [wordList count]){ 
    [self.textView setText: [NSString stringWithFormat:@"%@ %@",self.textView.text, [wordList objectAtIndex:wordIndex]]]; 
    wordIndex++; 
    [self resetTextAndFont]; 
    [self.textView setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15]]; 
    NSLog(@"Timer Fired!"); 
} 

else { 
    //[self.wordtimer invalidate]; 
    [self stopTimer]; 
    NSLog(@"Timer quit."); 

    [self resetTextAndFont]; 
    [self.textView setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15]]; 
    wordIndex = 0; 
} 

은}

난 그냥을 취소 textView의 텍스트와 [self startTimer]를 호출하고 무효화하려면 [self stopTimer]를 호출하면됩니다. Wordindex는 전역 정수입니다.

1

모든 것을 루프로 처리하는 대신 반복되는 NSTimer를 설정하고 발생시킬 때마다 텍스트보기에 새 단어를 추가하는 것이 좋습니다. 이를 통해 업데이트시기를보다 잘 제어 할 수 있으며 UI에서 다음 반복 전에 결과를 표시 할 수 있습니다. 마지막 단어가 표시되면 타이머를 무효화하십시오.

+0

나는이 이론의 일부를 읽을 것이다. 답변 해주셔서 감사합니다. –

0

클래스 문자열과 int 변수에 전역 변수를 취합니다. string은 fullString이고 int 변수는 wordIndex입니다. 사용자 지정 메서드를 호출하려면 NSTimer 또는 지연을 사용하십시오. 그 방법으로 체크를 넣으십시오.

if(wordIndex < [wordList count]){ 

NSString *newText = [NSString stringWithFormat:@"%@", [wordList objectAtIndex:wordIndex]]; 
// increment the global int variable by one i.e.wordIndex++ 
// append this string to exsting string 
// set appended text to textview... 
} 
+0

답해 주셔서 감사합니다. 나는 컴퓨터를 가지고 있지 않지만, 할 때 나는 이것을 시도하고 다시 당신에게 돌아갈 것입니다. –

1

단어 단위로 단어를 표시하려면 먼저 componentsSeparatedByCharactersInSet을 사용하여 공백 문자로 문장을 분할해야합니다. 그러면 배열을 반환하고 이후에 지연 방식으로 추출해야하는 것과 동일한 배열을 반환합니다. NSTimer를 사용하여 수행과 같은 수 있습니다 UITexyView 아래에 그 -

// 아래는 타이머를 사용하여 지연

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    timer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(startTimer) userInfo:nil repeats:YES]; 
} 

// 아래는 타이머 방법

-(void)startTimer 
{ 
    //Assuming some string 
    NSString *[email protected]"This is testing"; 

    //Splitting the sentence by space character 
    NSArray *arr=[str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    //len is integer which is a counter for iteration of array. 
    if (len<arr.count) 
    { 
    //Appending word by word 
     [[self.textView textStorage] appendAttributedString:[[NSAttributedString alloc]initWithString:arr[len]]]; 
     [[self.textView textStorage] appendAttributedString:[[NSAttributedString alloc]initWithString:@" "]]; 
     len++; 
    } 
    else 
    { 
     [timer invalidate]; 
     return; 
    } 
} 
,536입니다
관련 문제