简体   繁体   中英

Make the specific string Pattern bold and blue in UILable

i have a program in which i get a tweets from twitter and show them in UITableviewcell . now problem is that i have to make a all twitter names bold and bule and show them in the orginal tweet with bule and bold names. For Example i have tweet like this

MT @OraTV : SNEAK PEEK: @tomgreenlive @TheoVon & @DavidBegnaud talk Miley's #twerking #Batfleck &more on

so all the names starting with @ should be bold and bule.

i use this code to extract All names starting with @ but not know how to bold them and show them in single 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]; 
}

So you have all the names properly extracted already? If so, it seems like NSAttributedString is what you want. More information here .

Something like this: [str setTextColor:[UIColor blueColor] range:NSMakeRange(0,5)];
For bold text, use [UIFont boldSystemFontOfSize:fontSize] . See example in the second link above.

you have to build an NSAttributedString by swiping between 2 fonts and colors.

If you're able to detect them, you should probably replace your names by surrounding them with a known markup (eg: @aName). Then, parse the string to build a NSAttributedString.

You can use this code (not tested, you'll probably have to tweak):

// 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;
        }
    }
}

Hope that helps.

Cyril

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM