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

发布于 2024-11-17 10:53:47 字数 1688 浏览 0 评论 0原文

我有一个 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行中的最后一个?

I have a TextView and I need to add a button above each word with the same size and the same font of the button title as in current word. This code works, but if line has one word for example it does not.

.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];

    }

}

How to define that the word is the last one in the line of UITextView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-11-24 10:53:47

您必须在这里进行简单的条件检查。

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

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

    sourceArray = [NSArray arrayWithObject:sourceString];
}

编辑:

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

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

我希望这应该有效。

You have to do a simple condition checking here.

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

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

    sourceArray = [NSArray arrayWithObject:sourceString];
}

Edit:

TextView will contain both white space and new line characters. So you can use the componentsSeparatedByCharactersInSet: method to separate the text. Just use,

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

I hope this should work.

猫七 2024-11-24 10:53:47

这段代码对我有用



@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;

    }

}

This code works for me



@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;

    }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文