UITextView - 如何定义一个单词是一行中的最后一个单词

时间:2011-06-27 09:05:07

标签: iphone objective-c ios xcode uitextview

我有一个TextView,我需要在每个单词上方添加一个按钮,其大小和按钮标题的字体与当前单词相同。此代码有效,但如果行有一个单词,例如它没有。

.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行中的最后一个单词?

2 个答案:

答案 0 :(得分:1)

你必须在这里做一个简单的条件检查。

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

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

    sourceArray = [NSArray arrayWithObject:sourceString];
}

修改

TextView将包含空格和换行符。因此,您可以使用componentsSeparatedByCharactersInSet:方法分隔文本。只需使用,

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

我希望这应该有用。

答案 1 :(得分: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;

    }

}