繁体   English   中英

在UILable中使特定的字符串Pattern为粗体和蓝色

[英]Make the specific string Pattern bold and blue in UILable

我有一个程序,其中我从Twitter获得推文,并在UITableviewcell显示它们。 现在的问题是,我必须将所有推特名称设为粗体和bule,并在原始推文中以bule和粗体名称显示它们。 例如我有这样的推文

MT @OraTV :偷窥: @tomgreenlive @TheoVon & @DavidBegnaud谈论麦莉的#twerking #Batfleck &more内容

因此,所有以@ should be bold and bule.开头的名称@ 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]; 
}

因此,您已经正确提取了所有名称? 如果是这样,您似乎想要的是NSAttributedString 更多信息在这里

像这样: [str setTextColor:[UIColor blueColor] range:NSMakeRange(0,5)];
对于粗体文本,请使用[UIFont boldSystemFontOfSize:fontSize] 请参阅上面第二个链接中的示例。

您必须通过在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;
        }
    }
}

希望能有所帮助。

西里尔

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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