2011-06-27 11 views
0

私はTextViewを持っており、現在の単語と同じサイズと同じフォントで各単語の上にボタンを追加する必要があります。このコードは機能しますが、行に1つの単語がある場合などはありません。UITextView - 単語が行の最後の単語であることを定義する方法

.h 
@interface Text2ButtonsViewController : UIViewController { 

    UITextView *sourceText; 
} 
@property (nonatomic, retain) IBOutlet UITextView *sourceText; 

.m 

-(IBAction) processText { 

    for (UIView *button in [self.sourceText subviews]) { 
     if (button.tag == 33) 
      [button removeFromSuperview]; 
    } 

    NSString *sourceString = [NSString stringWithString:self.sourceText.text]; 

    NSArray *sourceArray = [sourceString componentsSeparatedByString:@" "]; 

    CGSize spaceSize = [@" " sizeWithFont:self.sourceText.font]; 

    float textPadding = 8.0f; 

    float stepX = textPadding; 
    float stepY = textPadding; 
    CGRect buttonFrame; 

    for (NSString *string in sourceArray) { 

    UIButton *actButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    actButton.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5]; 

    [actButton setTitle:string forState:UIControlStateNormal]; 
    actButton.titleLabel.font = self.sourceText.font; 
    CGSize stringSize = [string sizeWithFont:self.sourceText.font]; 
    actButton.tag = 33; 
    //if summary width of all buttons and spaces are greater than 
    if (stepX + stringSize.width + textPadding > self.sourceText.frame.size.width) { 
     stepX = textPadding; 
     stepY = stepY + stringSize.height; 
    } 
    buttonFrame = CGRectMake(stepX, stepY, stringSize.width, stringSize.height); 

    stepX = stepX + stringSize.width + spaceSize.width; 
    actButton.frame = buttonFrame; 

    [self.sourceText addSubview:actButton]; 

    } 

} 

単語がUITextViewの行の最後のものであることを定義する方法はありますか?

答えて

1

ここで簡単な条件を確認する必要があります。

NSArray *sourceArray = [sourceString componentsSeparatedByString:@" "]; 

if ([sourceArray count] == 0 && [sourceString length] != 0) { 

    sourceArray = [NSArray arrayWithObject:sourceString]; 
} 

編集:

TextViewには、ホワイトスペースと改行文字の両方が含まれます。したがって、componentsSeparatedByCharactersInSet:メソッドを使用してテキストを区切ることができます。ちょうど使用、

NSArray *sourceArray = [sourceString componentsSeparatedByCharactersInSet:[NSCharacterSet whiteSpaceAndNewLineCharacterSet]]; 

私はこれがうまくいけばいいです。

+0

申し訳ありませんが動作しますが、私はあなたが何を意味するかキャッチしません。 [sourceArray count] == 0の場合、ソース文字列が空であることを意味します。そして、[sourceString count]は動作しません。 – Michael

+0

@Michael、はい。 textViewに複数の単語が含まれている場合、それらの単語の間にスペース '@" "があり、' [sourceString componentsSeparatedByString:@ ""] 'はそれらの単語を配列として返します。しかし、textViewに1つの単語しか含まれていない場合は、テキストにスペースがないので、 '[sourceString componentsSeparatedByString:@" "]'は 'nil'を返します。したがって、 '[sourceArray count]'は '0'になります。その場合、 '[sourceString count]'が '0'でなければ、textViewに単語があります。右?これは、if文のif([sourceArray count] == 0 && [sourceString count]!= 0) 'が動作する方法です。 – EmptyStack

+0

@EmptyStackまず、私の質問に注意を払うことに感謝します。しかし、NSStringクラスにはメソッドカウントがありません。私の質問がはっきりしていないのは私のせいです。つまり、「bla bla bla bla bla bla bla bla」のようなテキストがあれば、それは機能します。テキストが "bla bla bla bla bla bla bla bla bla \ nbla \ n bla bla"の場合 - それはしません。私はUITextViewが現在の単語で新しい行を開始したという通知を得る必要があります。 – Michael

0

このコードは私のために



@interface DetailViewController : UIViewController { 

    UITextView *sourceText; 
    UIView *buttonHolder; 
} 

@implementation DetailViewController 

- (IBAction) processText { 

    self.buttonHolder.hidden = NO; 

    self.sourceText.editable = NO; 

    self.buttonHolder.frame = CGRectMake(0.0f, 0.0f, self.sourceText.contentSize.width, self.sourceText.contentSize.height); 

    NSLog(@"self.buttonholder.frame = %@", NSStringFromCGRect (self.buttonHolder.frame)); 

    if ([[self.buttonHolder subviews] count] !=0) { 
     for (UIView *button in [self.buttonHolder subviews]) 
      [button removeFromSuperview]; 

    } 

    CGSize spaceSize = [@" " sizeWithFont:self.sourceText.font]; 

    float textPadding = 8.0f; 

    float stepX = textPadding; 
    float stepY = textPadding; 
    CGRect buttonFrame; 
    CGSize wordSize; 
    NSString *sourceString = [NSString stringWithString:self.sourceText.text]; 

    float lineHeight = [sourceString sizeWithFont:self.sourceText.font].height; 

    NSArray *linesArray = [sourceString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 

    for (NSString *line in linesArray) { 

     NSArray *wordsInLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 

     for (NSString *word in wordsInLine) { 

      UIButton *actButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

      actButton.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5]; 

      [actButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 

      [actButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; 

      [actButton setTitle:word forState:UIControlStateNormal]; 

      [actButton addTarget:self action:@selector(workWithWord:) forControlEvents:UIControlEventTouchUpInside]; 

      actButton.titleLabel.font = self.sourceText.font; 

      wordSize = [word sizeWithFont:self.sourceText.font]; 
      actButton.tag = 33; 

      if (stepX + wordSize.width + textPadding > self.sourceText.frame.size.width) { 
       stepX = textPadding; 
       stepY = stepY + lineHeight; 
      } 

      buttonFrame = CGRectMake(stepX, stepY, wordSize.width, lineHeight-lineHeight/10.0); 
      actButton.frame = buttonFrame; 

      stepX = stepX + wordSize.width + spaceSize.width; 

      [self.buttonHolder addSubview:actButton]; 

     } 

     stepX = textPadding; 
     stepY = stepY + lineHeight; 

    } 

} 

関連する問題