petrpavlik / PPLabel

UILabel subclass with detection of touches on individual characters
90 stars 26 forks source link

The program will crash if label's attributeText without NSParagraphStyle attribute #6

Open wangqinggong opened 8 years ago

wangqinggong commented 8 years ago

In the function - (CFIndex)characterIndexAtPoint:(CGPoint)point {} .I think it is necessary to add a judgement, because not every attributeText must have NSParagraphStyle attribute. i have tested that if an attributeText does not have NSParagraphStyle attribute,there will be a crash in this function. Following is my suggestion:

`[self.attributedText enumerateAttribute:(NSString)kCTParagraphStyleAttributeName inRange:NSMakeRange(0, [optimizedAttributedText length]) options:0 usingBlock:^(id value, NSRange range, BOOL stop) {

    if(value != nil)
    {
        NSMutableParagraphStyle *paragraphStyle = [value mutableCopy];

        if ([paragraphStyle lineBreakMode] == kCTLineBreakByTruncatingTail) {
            [paragraphStyle setLineBreakMode:kCTLineBreakByWordWrapping];
        }

        [optimizedAttributedText removeAttribute:(NSString*)kCTParagraphStyleAttributeName range:range];
        [optimizedAttributedText addAttribute:(NSString*)kCTParagraphStyleAttributeName value:paragraphStyle range:range];
        }

}];

`