0

嗨,我有 2 个标签 textview 和 labelsText,在这种方法中它们是 string1 和 string2,但我真的无法让 textview string1 和 labelsText string2 工作

这是代码:

-(void)findEqualsIn:(NSString *)string1 and:(NSString *)string2 {
    NSMutableArray *string1chars = [[NSMutableArray alloc] init];
    NSMutableArray *string2chars = [[NSMutableArray alloc] init];


    //filling the string1chars array
    for (int i = 0; i < [string1 length]; i++) {
        [string1chars addObject:[NSString stringWithFormat:@"%c", [string1 characterAtIndex:i]]];
    }

    //filling the string2chars array
    for (int i = 0; i < [string2 length]; i++) {
        [string2chars addObject:[NSString stringWithFormat:@"%c", [string2 characterAtIndex:i]]];
    }

    //checking if they have some letters in common on the same spot
    for (int i = 0; i < [string1chars count] && i < [string2chars count]; i++) {
        if ([[string1chars objectAtIndex:i] isEqualToString:[string2chars objectAtIndex:i]]) {
            //change the color of the character at index i to green
        } else {
            //change the color of the character at index i to the standard color

        }
    }
4

3 回答 3

0

uilabela or中的所有文本uitextview必须是相同的字体/颜色。

于 2012-04-23T20:32:26.367 回答
0

由于 iOS SDK 中没有内置的富文本组件,因此仍然很少有可用的实现。我建议您使用FTCoreText来创建 UILabels(不可编辑)和可编辑的EGOTextView

于 2012-04-23T20:45:10.427 回答
0

您可以添加多个标签,其中每个字母都包含在其中,并使其看起来像是仅在 1 个标签中。这将需要这样的东西:

//make sure the first word is always the one the user gave so you don't show him/her the hidden string
- (void)outputColoredLabelsTo:(UIView *)theView with:(NSString *)word1 and:(NSString *)word2 {
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 0, 0)] autorelease];

    for (int i = 0; i < [word1 length] && i < [word2 length]; i++) {
        NSString *value = [NSString stringWithFormat:@"%c", [word1 characterAtIndex:i]];
        CGSize size = [value sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
        label = [[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x + label.frame.size.width, label.frame.origin.y, size.width, size.height)];
        [label setText:value];

        if ([word1 characterAtIndex:i] == [word2 characterAtIndex:i]) {
            NSLog(@"Match found at index %i (letter %c)", i, [word1 characterAtIndex:i]);
            [label setTextColor:[UIColor greenColor]];
        } else {
            NSLog(@"No match found at index %i (printing letter %c)", i, [word1 characterAtIndex:1]);
            [label setTextColor:[UIColor blackColor]];
        }

        [theView addSubview:label];
    }
}
于 2012-04-24T08:20:14.493 回答