AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString
https://github.com/AliSoftware/OHAttributedLabel/wiki
1.51k stars 344 forks source link

Underlining text issue #150

Closed sankaet closed 10 years ago

sankaet commented 11 years ago

I am using the following code and lets say if the string is sank_aet it automatically makes sankaet (with aet underlined). I am not sure why is it doing that. Here is the code

        gifString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:@"[%@ @%@](openUser:%@)",post[@"user_fullname"],post[@"user_username"],post[@"user_username"]]];
        [gifString setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0]];
        [gifString setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0] range:NSMakeRange([post[@"user_fullname"] length]+1,[post[@"user_username"] length]+3)];
        [gifString setTextColor:[UIColor whiteColor]];
        [gifString setTextAlignment:kCTTextAlignmentLeft lineBreakMode:kCTLineBreakByTruncatingTail];
        [OHASBasicMarkupParser processMarkupInAttributedString:gifString];
        cell.userFullName.attributedText = gifString;
AliSoftware commented 10 years ago

It does that because of OHASBasicMarkupParser interpret _ as the markup to underline text it surrounds.

What is odd tough is that it does that even if you don't have a `at the end of the text. Maybe an issue with the RegEx used inOHASBasicMarkupParser, but I can't see why, as the regex used is(.+?)which clearly expects an underscore at the end so I don't get what is causing it… Are you sure you don't have any_` later in your text?

mstzn commented 10 years ago

I've same problem too. Also im using OHAttributedLabel for mention and hashtags. But if i ve a text having underscore, all the texts in label be underline. I'm using "(?:\s|\A)[##]+([A-Za-z0-9-_]+)" regex for hashtag. Such as the post content like:

"6 October 2013 , 40-years-old passed on the latest wars fought by #Egypt ,and Egypt still in a state of disarray , and the #Egyptian_citizen still looking forward to a better life of life lived for forty past year , many wars waiting for the Egyptian Citizen, in order to better #education , #health and the #necessities_of_life , Still long way to go ."

Everything normal while "#Egyptian_citizen" hashtag. It's showing it as "#Egyptiancitizen" and underlining starting with "citizen" world and ending with other underscore..

Is it not supporting hashtags with underscore?

How can I solve this problem ?

mstzn commented 10 years ago

I'm temporary solved this problem with replacing underscore with three underscore...

AliSoftware commented 10 years ago

I'll try to add word boundary modifiers \\b around the _(.+?)_ regex, so that _xxx_ only underline the xxx text if it is an isolated word (surrounded with spaces, end-on-line, carriage return, line feed, whitespace, punctuation… but not another word-character). I'll probably do the same for the bold markup *xxx*.

AliSoftware commented 10 years ago

You really DO need the Markup parser in addition to your standard hashtag detection? Couln't the hashtag detection suffice?

AliSoftware commented 10 years ago

Fixed in 3.5.0.