OHAttributedLabel:用于URL的setLink

时间:2012-12-05 17:15:55

标签: objective-c ios

我需要显示如下链接: sitename.com/abc.html 在OHAttributedLabel上。我想为这个网址设置setLink: userlink:sitename.com/abc.html

然后我点击标签上的链接,在UIWebView委托上返回两次单击操作,一个带有URL,另一个没有URL ....

我只需要一次点击操作

我该怎么办?

更新

////seeingUrlString = @"sitename.com/abc.html";
[_tmpContentAttributeString setLink:[NSURL URLWithString:seeingUrlString] range:hyperLinkRange];

    self.contentLabel.attributedText = _tmpContentAttributeString;


-(BOOL)attributedLabel:(OHAttributedLabel *)attributedLabel shouldFollowLink:(NSTextCheckingResult *)linkInfo
{

    CustomWebView *_customWebViewa = [[CustomWebView alloc] init];
    _customWebViewa.webView.scrollView.scrollEnabled = NO;
    _customWebViewa.webView.scrollView.bounces = NO;
    _customWebViewa.webView.frame = self.view.bounds;

    NSString *_url = [linkInfo.URL absoluteString];
    if (![_url hasPrefix:HTTPPrefix]) {
        _url = [HTTPPrefix stringByAppendingString:_url];
    }
    _url = [NSString stringWithFormat:@"%@%@%@", _url, Slash, NoFollow];
    [self loadWebView:_customWebViewa.webView url:_url];
     return NO;
}

两次点击一个来自OHAttributedLabel委托的行动,另一个我不知道它来自哪里......

UPDATE2:

实际上我只需要在OHAttributedLabel上显示链接(如:sitename.com/abc.html),但不要使用此链接,我希望用户可以看到这是可点击的链接,如果他们点击链接,则为OHAttributedLabel委托实际上启动了一个自定义链接网址,自定义链接网址就像:

userlink:sitename.com/abc.html

我希望OAHAtrributedLabel将显示网址视为普通字符串

1 个答案:

答案 0 :(得分:0)

OAHAtrributedLabel返回NO的委托方法。

-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo{
    <Do your work here>
    return NO;
}

修改 我无法运行你的代码。所以,我发布了与之合作的代码。

//forgotPasswordlabel is my OHAttributedLabel

self.forgotPasswordlabel.text = [[NSString localizedStringForKey:@"forgot_password"] uppercaseString];
self.forgotPasswordlabel.delegate = self;
    NSRegularExpression* userRegex = [NSRegularExpression regularExpressionWithPattern:[[NSString localizedStringForKey:@"forgot_password"] uppercaseString] options:0 error:nil];
    [userRegex enumerateMatchesInString:self.forgotPasswordlabel.text options:0 range:NSMakeRange(0,self.forgotPasswordlabel.text.length)
                             usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
     {
         // FORGOT_PASSWORD_URL is mu URL which I'm opening in my WebView
         [self.forgotPasswordlabel addCustomLink:[NSURL URLWithString:FORGOT_PASSWORD_URL] inRange:match.range]; // add it
     }];

委托方法中的ANd

-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo{
    [self forgetPassword:nil]; // here I handle password reset action
    return NO;
}

嘿,看看我的标签enter image description here

的出现

希望它有所帮助...