2013-05-29 31 views
1

昨天我发布了这个问题:Removing parentheses from the string in iOS。但是我仍然无法从标签中删除括号。在iOS中删除UILabel中的支架

不知道我的错误。花了整整一个晚上,仍然无法这样做。

我正在使用TTTAttributedLabel。我的代码如下所示:

-(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; 
} 

仍然无法删除括号。任何人都可以指出我的错误吗?真的很感激帮助。

+0

我想你指的支架,你试图删除这个字符, “[” 右 – aaronman

+0

说错误的话。编辑了这个问题。 – lakesh

+0

我看到你正在尝试做什么。我所做的是修改'MGMushParser'(GitHub上的一个库)来获得我想要的结果。它会解析某些“标签”(在你的案例中是[italic text]),并用一些属性替换它。 – borrrden

回答

2

尝试改变最后两个符合此二:

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

与字符串开头的方法...不改变本身只返回被改变一个新的字符串的字符串。

顺便说一下,NSString对象是不可变的。如果你想改变字符串,你可以使用NSMutableString,下面的实现只使用NSMutabeString,你已经在块中使用了。

-

试试这个:

-(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; 
} 
+0

它删除括号,但斜体字体被删除...现在如何确保两者都保留... – lakesh

+0

将设置文本的属性标签中的行放在方法的开头。 – ggrana

+0

我使用括号来区分斜体文本和非斜体文本。 – lakesh