1

Twitter からツイートを取得して に表示するプログラムがありますUITableviewcell。今の問題は、すべての Twitter の名前を太字と bule にして、元のツイートに bule と太字の名前で表示する必要があることです。たとえば、私はこのようなツイートをしています

MT @OraTV: SNEAK PEEK:@tomgreenlive @TheoVon & @DavidBegnaudマイリーの#twerking #Batfleck &more

だから、で始まるすべての名前@ should be bold and bule.

このコードを使用して、@ で始まるすべての名前を抽出しますが、それらを太字にして単一の uitableviewcell に表示する方法がわかりません

NSString * aString =twitterMessage
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanUpToString:@"@" intoString:nil]; 
while(![scanner isAtEnd]) {
NSString *substring = nil;
[scanner scanString:@"@" intoString:nil]; 
if([scanner scanUpToString:@" " intoString:&substring]) {

    [substrings addObject:substring];
}
[scanner scanUpToString:@"@" intoString:nil]; 
}
4

2 に答える 2

0

2 つのフォントと色の間をスワイプして NSAttributedString を作成する必要があります。

それらを検出できる場合は、名前を既知のマークアップ (例: @aName) で囲んで置き換える必要があります。次に、文字列を解析して NSAttributedString を作成します。

このコードを使用できます (テストされていないため、おそらく調整する必要があります)。

// String to parse
NSString *markup = @"MT <color>@OraTV</color>: SNEAK PEEK: <color>@tomgreenlive</color>...";

// Names font and color
UIFont *boldFont = [UIFont boldSystemFontOfSize:15.0f];
UIColor *boldColor = [UIColor blueColor];

// Other text font and color
UIFont *stdFont = [UIFont systemFontOfSize:15.0f];
UIColor *stdColor = [UIColor blackColor];

// Current font and color
UIFont *currentFont = stdFont;
UIColor *currentColor = stdColor;

// Parse HTML string
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:@""];
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"(.*?)(<[^>]+>|\\Z)"
                                                                  options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators
                                                                    error:nil];
NSArray *chunks = [regex matchesInString:markup options:0 range:NSMakeRange(0, [markup length])];

for (NSTextCheckingResult* b in chunks)
{
    NSArray *parts = [[markup substringWithRange:b.range] componentsSeparatedByString:@"<"];

    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:currentFont,NSFontAttributeName,currentColor,NSForegroundColorAttributeName,nil];
    [aString appendAttributedString:[[NSAttributedString alloc] initWithString:[parts objectAtIndex:0] attributes:attrs]];

    if([parts count] > 1)
    {
        NSString *tag = (NSString *)[parts objectAtIndex:1];
        if([tag hasPrefix:@"color"])
        {
            currentFont = boldFont;
            currentColor = boldColor;
        }
        else if([tag hasPrefix:@"/color"])
        {
            currentFont = stdFont;
            currentColor = stdColor;
        }
    }
}

それが役立つことを願っています。

シリル

于 2013-08-29T18:40:23.607 に答える
0

すべての名前が適切に抽出されていますか? もしそうなら、それはNSAttributedStringがあなたが望むもののようです。詳細はこちら

このようなもの:[str setTextColor:[UIColor blueColor] range:NSMakeRange(0,5)];
太字のテキストには、を使用します[UIFont boldSystemFontOfSize:fontSize]。上記の 2 番目のリンクの例を参照してください。

于 2013-08-29T18:33:30.340 に答える