如何在UITextView中的字符串中显示格式化的链接

时间:2015-07-30 06:29:16

标签: ios objective-c uitextview

在UITextView中,我添加了 UIDataDetectorTypeLink 来检测字符串中的链接,它运行正常。但问题是我有一个像www.google.com<http://www.google.com>这样的字符串,它显示为原样。我只想显示www.google.com。有没有办法实现这个目标?

修改

这不是特定于google.com。我举了一个谷歌的例子。字符串可能格式为Title<url>,也可能不格式为CookieManager.getInstance().setCookie(domain, value); CookieSyncManager.getInstance().sync(); 。我需要处理这种格式。

4 个答案:

答案 0 :(得分:0)

试试这个

NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"Google"];
[str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0, str.length)];
yourTextView.attributedText = str;

希望它有所帮助。

答案 1 :(得分:0)

您可以编写一个方法来执行此操作。编写方法可以使代码保持清洁和易于阅读。

- (NSString *)cutString:(NSString *)sourceString {
    NSRange range;
    range.location = 0;
    range.length = @"www.google.com".length;
    return [sourceString substringWithRange:range];
}

//call that method to subString
NSString myStr = [self cutString:@"www.google.com<http://www.google.com>"];

答案 2 :(得分:0)

试试这个我创建了一个带有NSScanner

的示例解析器
- (void)textFormattingForLink {

    NSString *inputString = @"some prefix string Google<www.google.com> and Stackoverflow<www.stackoverflow.com>";
    NSScanner *scanner = [NSScanner scannerWithString:inputString];
    NSMutableAttributedString *outputString = [[NSMutableAttributedString alloc] init];
    while (![scanner isAtEnd]) {
        NSString *prefixOfLink;
        [scanner scanUpToString:@"<" intoString:&prefixOfLink];

        __block NSString *lastWord = nil;

        [prefixOfLink enumerateSubstringsInRange:NSMakeRange(0, [prefixOfLink length]) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
            lastWord = substring;
            *stop = YES;
        }];
        NSScanner *preficScanner = [NSScanner scannerWithString:prefixOfLink];
        NSString *prefixOfLinkWord;
        [preficScanner scanUpToString:lastWord intoString:&prefixOfLinkWord];
        [outputString appendAttributedString:[[NSAttributedString alloc] initWithString:prefixOfLinkWord]];

        [scanner scanString:@"<" intoString:NULL];
        NSString *linkString;
        [scanner scanUpToString:@">" intoString:&linkString];
        NSMutableAttributedString * link = [[NSMutableAttributedString alloc] initWithString:lastWord];
        [link addAttribute: NSLinkAttributeName value: linkString range: NSMakeRange(0, link.length)];

        [outputString appendAttributedString:link];
        [scanner scanString:@">" intoString:NULL];
        [outputString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    }
    [self.textView setAttributedText:outputString];
}

输出为enter image description here

答案 3 :(得分:0)

@Akhilrajtr 感谢NSScanner的实现,它启发了这个NSAttributedString类。希望它可以帮助别人!

- (void)setStringWithLinks:(NSString*)string
{
    if (string.length == 0)  
        return;

    NSScanner *scanner = [NSScanner scannerWithString:string];

    while(![scanner isAtEnd])
    {
        NSString * textBeforeLink;
        [scanner scanUpToString:@"<a href=\"" intoString:&textBeforeLink];

        NSString * linkURL;
        [scanner scanString:@"<a href=\"" intoString:NULL];
        [scanner scanUpToString:@"\">" intoString:&linkURL];

        NSString * linkText;
        [scanner scanString:@"\">" intoString:NULL];
        [scanner scanUpToString:@"</a>" intoString:&linkText];
        [scanner scanString:@"</a>" intoString:NULL];


        [self appendAttributedString:[[NSAttributedString alloc] initWithString:textBeforeLink]];

        if (linkText.length >0)
        {
            NSMutableAttributedString * link = [[NSMutableAttributedString alloc] initWithString:linkText];
            NSRange foundRange = NSMakeRange(0, link.length);
            [link addAttribute:NSLinkAttributeName value: linkURL range: foundRange];
            [link addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:foundRange];
            [link addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:foundRange];

            [self appendAttributedString:link];

            [self appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
        }
    }
}