Domanda

I posted this question yesterday:Removing parentheses from the string in iOS. But I am not still able to remove the brackets from the label.

Not sure what my mistake. Spent the whole night figuring, still not able to do so.

I am using TTTAttributedLabel. My code looks like this:

-(TTTAttributedLabel*)setItalicTextForLabel:(TTTAttributedLabel*)attributedLabel fontSize:(float)Size
{
    [attributedLabel setText:[self.infoDictionary objectForKey:@"description"] afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
    {
        NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
        NSRegularExpression *regexp = ParenthesisRegularExpression();
        UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:Size];
        DLog(@"%@",italicSystemFont.fontName);
        CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
        [regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            if (italicFont) {
                [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:result.range];
                [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:result.range];
                CFRelease(italicFont);
            }
        }];

        return mutableAttributedString;
    }];
    [[attributedLabel.text stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""];
    return attributedLabel;
}

Still not able to remove the brackets. Can anyone point out my mistake? Would really appreciate the help.

È stato utile?

Soluzione

Try to change the last two line with this two:

[attributedLabel setText:[[attributedLabel.text stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""]];
return attributedLabel;

The methods that begins with string... does not change the string itself only returns a new string that is changed.

By the way, NSString objects are immutable. If you want to change strings you can use NSMutableString, the below implementation use only the NSMutabeString, that you are already using in the block.

-

Try This:

-(TTTAttributedLabel*)setItalicTextForLabel:(TTTAttributedLabel*)attributedLabel fontSize:(float)Size
{
    [attributedLabel setText:[self.infoDictionary objectForKey:@"description"] afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
    {
        NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
        NSRegularExpression *regexp = ParenthesisRegularExpression();
        UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:Size];
        DLog(@"%@",italicSystemFont.fontName);
        CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
        [regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            if (italicFont) {
                [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:result.range];
                [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:result.range];
                CFRelease(italicFont);
                NSRange range1 = NSMakeRange (result.range.location, 1); 
                NSRange range2 = NSMakeRange (result.range.location + result.range.length-2, 1); 
                [mutableAttributedString replaceCharactersInRange:range1 withString:@""];
                [mutableAttributedString replaceCharactersInRange:range2 withString:@""];
            }
        }];
        return mutableAttributedString;
    }];
    return attributedLabel;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top