在iOS App中检测哈希标记#,提及标签@ [英] Detect hash tags #, mention tags @, in iOS like in Twitter App

查看:78
本文介绍了在iOS App中检测哈希标记#,提及标签@的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测描述 UILabel 中的 #Tags ,并将文字颜色更改为 [UIColor BlueColor]; 我无法将 UILabel 中的特定文本颜色更改为Blue。现在我在自定义 UITableViewCell 中使用此 UILabel 。有没有办法解决这个问题,以区分 #Tags 和普通文本的文字颜色?任何人可以帮助我解决这个


解决方案

   - ?(NSMutableAttributedString *)decorateTags :( NSString *)stringWithTags {


NSError * error = nil;

//对于 维杰#Apple开发
NSRegularExpression *正则表达式= [NSRegularExpression regularExpressionWithPattern:@ #(\\w +) 选项:0错误:&安培;错误];

//对于 维杰@Apple开发
// NSRegularExpression *正则表达式= [NSRegularExpression regularExpressionWithPattern:@ @(\\w +) 选项:0错误:&安培;误差]。

NSArray * matches = [regex matchesInString:stringWithTags options:0 range:NSMakeRange(0,stringWithTags.length)];
NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:stringWithTags];

NSInteger stringLength = [stringWithTags length];

for(NSTextCheckingResult *匹配中匹配){

NSRange wordRange = [match rangeAtIndex:1];

NSString * word = [stringWithTags substringWithRange:wordRange];

//设置字体
UIFont * font = [UIFont fontWithName:@Helvetica-Bold尺寸:15.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,stringLength)];


//设置背景颜色
UIColor * backgroundColor = [UIColor orangeColor];
[attString addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:wordRange];

//设置前景颜色
UIColor * foregroundColor = [UIColor blueColor];
[attString addAttribute:NSForegroundColorAttributeName value:foregroundColor range:wordRange];

NSLog(@找到标签%@,字);

}

//设置文本字段或标签以显示结果

// yourTextField.attributedText = attString;
//
// yourLabel.attributedText = attString;

返回attString;
}


I need to Detect #Tags given in description UILabel and change text color as [UIColor BlueColor]; where i am not able to change particular text colors in UILabel to Blue. Now i am Using this UILabel in custom UITableViewCell. is there any way to solve this issue to differentiate #Tags and normal text by Text Colors ? can anybody help me to solve this ?

解决方案

-(NSMutableAttributedString*)decorateTags:(NSString *)stringWithTags{


    NSError *error = nil;

    //For "Vijay #Apple Dev"
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];

    //For "Vijay @Apple Dev"
    //NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"@(\\w+)" options:0 error:&error];

    NSArray *matches = [regex matchesInString:stringWithTags options:0 range:NSMakeRange(0, stringWithTags.length)];
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:stringWithTags];

    NSInteger stringLength=[stringWithTags length];

    for (NSTextCheckingResult *match in matches) {

        NSRange wordRange = [match rangeAtIndex:1];

        NSString* word = [stringWithTags substringWithRange:wordRange];

        //Set Font
        UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
        [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, stringLength)];


        //Set Background Color
        UIColor *backgroundColor=[UIColor orangeColor];
        [attString addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:wordRange];

        //Set Foreground Color
        UIColor *foregroundColor=[UIColor blueColor];
        [attString addAttribute:NSForegroundColorAttributeName value:foregroundColor range:wordRange];

        NSLog(@"Found tag %@", word);

    }

    // Set up your text field or label to show up the result

    //    yourTextField.attributedText = attString;
    //
    //    yourLabel.attributedText = attString;

    return attString;
}

这篇关于在iOS App中检测哈希标记#,提及标签@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆