1

我需要在 OHAttributedLabel 上显示如下链接:sitename.com/abc.html。我想为这个网址设置链接,例如: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 委托,另一个我不知道它来自哪里...

更新2:

实际上我只需要在 OHAttributedLabel 上显示一个链接(如:sitename.com/abc.html),但不要使用此链接,我希望用户可以看到这是可点击的链接,如果他们点击链接,OHAttributedLabel 代表实际上会触发建立一个自定义链接 url,自定义链接 url 是这样的:

用户链接:sitename.com/abc.html

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

4

1 回答 1

0

OAHAtrributedLabelreturn的委托方法中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
     }];

在委托方法中

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

熙是看看我的标签的外观在此处输入图像描述

希望能帮助到你...

于 2012-12-05T17:19:05.230 回答