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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值