iOS面试iOS Developer

iOS图文混排

2016-11-23  本文已影响0人  bd8915df25f2

有时候需要在系统cell的模板上做少许修改,可以用图文混排的方法处理cell.textLabel以及cell.detailLabel的字体样式,颜色以及加上图片等,
以下以设置cell.detailLabel为例:
系统默认
cell.detailTextLabel.text=[NSString stringWithFormat:@"date:%@",exam.date];
效果如图:

Snip20161123_1.png

    //设置需要修改的文本
    NSMutableAttributedString *detailString=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"date:%@",exam.date]];
    //设置需要修改的范围以及属性值
    [detailString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 4)];
    [detailString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
    [detailString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
    //设置data的值
    [detailString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, 11)];
    [detailString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(4, 11)];
    [detailString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(4, 11)];
    //添加图片
    NSTextAttachment *detailAttach=[[NSTextAttachment alloc]init];
    detailAttach.image=[UIImage imageNamed:@"login"];
    detailAttach.bounds=CGRectMake(0, 0, 15, 15);

    NSAttributedString *detailAttachString=[NSAttributedString attributedStringWithAttachment:attach];
    
    [detailString appendAttributedString:detailAttachString];

    cell.detailTextLabel.numberOfLines=0;
    cell.detailTextLabel.attributedText=detailString;

效果如图:


Snip20161123_2.png
上一篇下一篇

猜你喜欢

热点阅读