NSURL absoluteString的NSString无法获得范围

时间:2018-03-24 13:17:10

标签: ios object nsstring nsurl nsdatadetector

我正在开发聊天应用程序,其中输入的文本必须在其URL中被检测到。如果是这样,请更改其颜色并加下划线。请看下面的截图:

color

要更改我在代码后使用的网址的颜色:

NSString *urlString = [[detetctedURL absoluteString] stringByRemovingPercentEncoding];

            /* *detetctedURL is detected url from entered text using NSDataDetector
            /* *for messageText http://stackoverflow.com/somefolder/any-[thing]-etc, the detectedURL is http://stackoverflow.com/somefolder/any-%5Bthing%5B-etc */

            NSRange r = [messageText rangeOfString:urlString];
            if (r.location != NSNotFound)
            {
                 //colorFromHex 4285f4
                 [atext addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:66.0/255.0 green:133.0/255.0 blue:244.0/255.0 alpha:1.0] range:r];
                 [atext addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:r];
                 //set attributed text (atext) to UILabel               
            }

如果messageText包含同时具有特殊字符和百分比编码的URL,或者只包含百分比编码,那么如何正确格式化检测到的URL?

谢谢!

更新:

借助以下代码,我能够获得所需的范围。但是,对于大多数链接来说,它的工作几乎没有问题,但是如果有像']这样的字符,那就不是全部了 - ('。

NSString *urlString = [url absoluteString];
        NSRange r = [messageText rangeOfString:urlString];
        BOOL foundRange = YES;
        if (r.location == NSNotFound)
        {
            foundRange = NO;
            //for umlauts or special characters
            urlString = [[url absoluteString] stringByRemovingPercentEncoding];
            r = [messageText rangeOfString:urlString];
            if (r.location == NSNotFound)
            {
                //for white space in url
                urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
                r = [messageText rangeOfString:urlString];

                if (r.location == NSNotFound)
                {
                    urlString = [url absoluteString];
                    NSString *prefix = url.scheme;
                    if(prefix)
                    {
                        prefix = [prefix stringByAppendingString:@"://"];
                        urlString = [urlString stringByReplacingOccurrencesOfString:prefix withString:@""];
                        r = [messageText rangeOfString:urlString];
                        if (r.location == NSNotFound)
                        {
                            urlString = [urlString stringByRemovingPercentEncoding];
                            r = [messageText rangeOfString:urlString];
                            if (r.location == NSNotFound)
                            {
                                urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
                                r = [messageText rangeOfString:urlString];
                                if (r.location != NSNotFound){ foundRange = YES; }
                            }else{ foundRange = YES; }
                        }else{ foundRange = YES; }
                    }
                }else{ foundRange = YES; }
            }else{ foundRange = YES; }
        }

        if (foundRange)
        {
            //colorFromHex 4285f4
            [atext addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:66.0/255.0 green:133.0/255.0 blue:244.0/255.0 alpha:1.0] range:r];

            [atext addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:r];

            myLabel.attributedText = atext; 
        }

2 个答案:

答案 0 :(得分:0)

使用<?xml version="1.0" encoding="utf-8"?> <body> <sec id="sec1"> <title>Introduction</title> <p>Tuberculosis is associated with high mortality rate although according to the clinical trials that have been documented</p> <sec id="sec1.2"> <title>Related Work</title> <p>The main contributions in this study are: <list list-type="ordered"> <list-item><label>I.</label><p>Introducing SURF features descriptors for TB detection which for our knowledge has not been used in this problem before.</p></list-item> <list-item><label>II.</label><p>Providing an extensive study of the effect of grid size on the accuracy of the SURF.</p></list-item> </list></p> </sec> <sec id="sec1.3"> <title>Dataset</title> <p>The dataset used in this work is a standard computerized images database for tuberculosis gathered and organized by National Library of Medicine in collaboration with the Department of Health and Human Services, Montgomery County, Maryland; USA <xref ref-type="bibr" rid="ref15">[15]</xref>. The set contains 138 x-rays, 80 for normal cases and 58 with TB infections. The images are annotated with clinical readings comes in text notes with the database describing age, gender, and diagnoses. The images comes in 12 bits gray levels, PNG format, and size of 4020*4892. The set contains x-ray images information gathered under Montgomery County&#x0027;s Tuberculosis screening program.</p> <sec id="sec1.3.5"> <sec id="sec1.3.5.2"> <title>Methodologies</title> </sec> </sec> </sec> </sec> <sec id="sec2"> <p>The majority of TB and death cases are in developing countries.</p> <sec id="sec2.5"> <p>The disordered physiological manifestations associated with TB is diverse and leads to a complex pathological changes in the organs like the lungs.</p> <sec id="sec2.5.3"> <sec id="sec2.5.3.1"> <p>The complexity and diversity in the pulmonary manifestations are reported to be caused by age.</p> <sec id="sec2.5.3.1.1"> </sec> </sec> </sec> </sec> </sec> </body> 查找并突出显示网址。

NSDataDetector

您可以使用它来查找电话号码,地址等。

答案 1 :(得分:0)

在文本视图中设置.dataDetectorTypes,而不是尝试自己更改颜色。 UITextView将负责突出显示自己。