UITextView中链接的不同样式

时间:2015-03-24 09:10:16

标签: ios objective-c uitextview nsattributedstring

我希望我的UITextView包含纯文本,链接,电话号码和其他UIDataDetectorTypes。例如,纯文本 - 黑色,链接 - 带下划线的蓝色,主题标签 - 绿色。所以,我解析文本并添加到主题标签的链接:

[attributedString addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"hashtag://%@", hashtag] range:range];

如果dataDetectorTypes设置为UIDataDetectorTypeAll,则默认情况下会检测到其他链接。但是如何更改主题标签的链接样式?

3 个答案:

答案 0 :(得分:3)

您可以使用官方Twitter库跟踪名为twitter text的标签以及TTTAttributedLabel

Hashtags有特殊条件,这些条件可能超过以#开头的字母数字字符。

twitter文本库将自动为您找到主题标签的范围,然后您应该能够使用TTTAttributedLabel格式化它。

以下是两个库的示例实现。

- (void)scanForHashtag:(NSArray *)hashtags fromLabel:(id)sender
{
    TTTAttributedLabel *label = (TTTAttributedLabel *)sender;

    for (TwitterTextEntity *entity in hashtags) {
        UIFont *boldSystemFont = [UIFont fontWithName:@"Helvetica-Bold" size:12];
        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName,(NSString *)kCTFontAttributeName, nil];
        UIColor *hashtagColor;
        hashtagColor = [UIColor greenColor];
        NSArray *objects = [[NSArray alloc] initWithObjects:hashtagColor,[NSNumber numberWithInt:kCTUnderlineStyleNone],(__bridge id)font, nil];
        NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
        label.linkAttributes = linkAttributes;

        NSString *hashtag = [label.text substringWithRange:entity.range];

        if (entity.type == 2) { //Hashtag
            [label addLinkToPhoneNumber:hashtag withRange:entity.range];
        } else if (entity.type == 0) { //URL
            NSURL *url = [NSURL URLWithString:[label.text substringWithRange:entity.range]];
            [label addLinkToURL:url withRange:entity.range];
        }
    }
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{

    //Do whatever you need when the hashtag is tapped.
}

[self scanForHashtag:[TwitterText hashtagsInText:@"The text that contains the tweet with hashtags." checkingURLOverlap:NO] fromLabel:yourLabel]; //yourLabel is the label that contains the text so it would be formatted.

编辑UITextView

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
[attributedString addAttribute:NSBackgroundColorAttributeName
                   value:[UIColor yellowColor]
                   range:entity.range];

答案 1 :(得分:0)

您需要找到 hashtags 范围,然后为它们添加特殊属性。 例如。

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"YOUR HASHTAG PATTERN" options:0 error:&error];
if (!error) {
    [regex enumerateMatchesInString:messageText options:0 range:NSMakeRange(0, messageText.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        [mutableAttributedString addAttributes:@{
                                               NSFontAttributeName: [UIFont fontLinkMessageCell],
                                               (__bridge NSString*) kCTForegroundColorAttributeName: [UIColor colorLinkOutCell],
                                               NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone),
                                               } range:result.range];
    }];
}

答案 2 :(得分:0)

self.textView.linkTextAttributes = @{};
self.textView.textStorage.delegate = self;

- (void) textStorage: (NSTextStorage*) textStorage didProcessEditing: (NSTextStorageEditActions) editedMask range: (NSRange) editedRange changeInLength: (NSInteger) delta
{
    [textStorage enumerateAttribute: NSLinkAttributeName
                            inRange: editedRange
                            options: 0
                         usingBlock: ^(id value, NSRange range, BOOL* stop)
                         {
                             if (value)
                             {
                                 [textStorage addAttribute: NSForegroundColorAttributeName
                                                     value: [UIColor redColor]
                                                     range: range];
                             }
                         }];
}