DTAttributedLabel 使用方法

UILabel不能调整行距,改变文字颜色,响应文字点击。
DTAttributedLabel可以满足这些需求,这个组件在DTCoreText框架中,在github上下载即可。

DTAttributedLabel* noteContentLabel = [[DTAttributedLabel alloc] initWithFrame:CGRectMake(0,0, 100, 50)]; //创建对象
[noteContentLabel setLineBreakMode:UILineBreakModeWordWrap];
noteContentLabel.delegate = self;//设置委托对象
[noteContentLabel release];

设置DTAttributedLabel的排版格式:
目前只设置了行间距和行高
其它设置可以查阅 CTParagraphStyleSpecifier的定义
typedef CF_ENUM(uint32_t, CTParagraphStyleSpecifier) {
    kCTParagraphStyleSpecifierAlignment = 0,
    kCTParagraphStyleSpecifierFirstLineHeadIndent = 1,
    kCTParagraphStyleSpecifierHeadIndent = 2,
    kCTParagraphStyleSpecifierTailIndent = 3,
    kCTParagraphStyleSpecifierTabStops = 4,
    kCTParagraphStyleSpecifierDefaultTabInterval = 5,
    kCTParagraphStyleSpecifierLineBreakMode = 6,
    kCTParagraphStyleSpecifierLineHeightMultiple = 7,
    kCTParagraphStyleSpecifierMaximumLineHeight = 8,
    kCTParagraphStyleSpecifierMinimumLineHeight = 9,
    kCTParagraphStyleSpecifierLineSpacing = 10,	 /* deprecated */
    kCTParagraphStyleSpecifierParagraphSpacing = 11,
    kCTParagraphStyleSpecifierParagraphSpacingBefore = 12,
    kCTParagraphStyleSpecifierBaseWritingDirection = 13,
    kCTParagraphStyleSpecifierMaximumLineSpacing = 14,
    kCTParagraphStyleSpecifierMinimumLineSpacing = 15,
    kCTParagraphStyleSpecifierLineSpacingAdjustment = 16,
    kCTParagraphStyleSpecifierLineBoundsOptions = 17,

    kCTParagraphStyleSpecifierCount
};

    CGFloat gapHeight = DT_LABEL_GAPH;
    CTParagraphStyleSetting gap;
    gap.spec = kCTParagraphStyleSpecifierLineHeightMultiple;
    gap.value = &gapHeight;
    gap.valueSize = sizeof(float);

    CGFloat maxHeight = DT_LINE_MAX_HEIGHT;
    CTParagraphStyleSetting maxLineHeight;
    maxLineHeight.spec = kCTParagraphStyleSpecifierMaximumLineHeight;
    maxLineHeight.value = &maxHeight;
    maxLineHeight.valueSize = sizeof(float);
    
    CTParagraphStyleSetting settings[] = {
        gap,     //行间距
        maxLineHeight  //最大行高,如果不设这个遇到特殊字符会打乱行间距
    };
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings,sizeof(settings)/sizeof(CTParagraphStyleSetting));
    NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attributeStr addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0, [attributeStr length])];
    CFRelease(style);
    //这样还是有一些问题,比如emoji字符在其中会显得很大,我的解决方案是找到emoji字符的位置,然后把字体设小3个单位

DTAttributedLabel 使用 setAttributedString函数 设置文字,需要传入一个 NSMutableAttributedString对象,按照coreText的规则设置即可

[attributeStr addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIcolor redColor].CGColor range:NSMakeRange(0,10)]; //设置字体颜色,然后指定设置的范围
[attributeStr addAttribute:DTLinkAttribute value:@"LinkBtnClick" range:NSMakeRange(0,10)]; //0-10这一段文字就可以点击了
//要生成DTLinkButton对象还需要写DTAttributedLabel的委托方法
- (UIView *)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView viewForAttributedString:(NSAttributedString *)string frame:(CGRect)frame{
    NSDictionary *attributes = [string attributesAtIndex:0 effectiveRange:NULL];
    NSString *URLString = [attributes objectForKey:DTLinkAttribute];
    DTLinkButton *button = [[DTLinkButton alloc] initWithFrame:frame];
    //DTLinkButton继承于UIButton 在这里设置响应函数
    
    button.minimumHitSize = CGSizeMake(10, 10); // adjusts it's bounds so that button is always large enough
      return [button autorelease];
}



你可能感兴趣的:(2013年中技术总结)