在iOS中为文本的某个部分设置斜体文本

时间:2013-05-28 17:12:25

标签: ios objective-c uilabel nsregularexpression

我正在使用TTTAttributedLabel(https://github.com/mattt/TTTAttributedLabel)并使用它为括号中的那些设置斜体文本。问题是括号中的文本未设置为斜体。不确定我的错误在哪里..

代码如下所示:

static inline NSRegularExpression * ParenthesisRegularExpression() {
    static NSRegularExpression *_parenthesisRegularExpression = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _parenthesisRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\\([^\\(\\)]+\\)" options:NSRegularExpressionCaseInsensitive error:nil];
    });

    return _parenthesisRegularExpression;
}

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

                [mutableAttributedString removeAttribute:(NSString *)kCTForegroundColorAttributeName range:result.range];
                [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:result.range];
            }
        }];

        return mutableAttributedString;
    }];
    return attributedLabel;
}

我这样叫:mylabel = [self setItalicTextForLabel:descriptionLabel fontSize:23 stringToBeSet:string];

需要一些关于我做错的指导。

1 个答案:

答案 0 :(得分:3)

我从未使用TTTAttributedLabel,而我对NSAttributedString的体验来自于在OS X上玩NSTextView

那就是说,为什么不重构代码以将NSAttributedString直接传递给setText:afterInheritingLabelAttributesAndConfiguringWithBlock:?那种情况似乎是made possible two years ago

事实上,为什么不使用setText:

  

设置标签显示的文字。

     

@param text 标签要显示的NSStringNSAttributedString对象。如果指定的文本是NSString,则标签将显示文本,如UILabel,继承标签的文本样式。如果指定的文本是NSAttributedString,则标签文本样式将被属性字符串中指定的样式覆盖。

     

@discussion 此方法会覆盖UILabel -setText:以接受NSStringNSAttributedString个对象。默认情况下,此字符串为nil

我冒昧地重命名你的方法并清理代码。当然,没有测试,考虑到我没有TTTAttributedLabel可用,并且懒得设置一个演示项目只是为了回答这个问题: - )

-(void)applyItalicTextInParenthesisOfString:(NSString*)string
                                    toLabel:(TTTAttributedLabel*)attributedLabel
                                     fontSize:(float)size
{
    NSMutableAttributedString * mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
    NSRegularExpression *regexp = ParenthesisRegularExpression();

    UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:size];
    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)
        {
            // No need to remove an attribute; the string was already an
            // NSString, without any attributes.
            // [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:result.range];
            // [mutableAttributedString removeAttribute:(NSString *)kCTForegroundColorAttributeName range:result.range];

            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:result.range];
            [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:result.range];
        }
    }];

    CFRelease(italicFont);

    [attributedLabel setText:string]
}

我也有点过时了,所以为了便于阅读,我会避免使用ARC和阻止它。