富文本简单设置、加虚线边框

    NSString *text = @"守夜人誓言:「长夜将至,我从今开始守望,至死方休。我将不娶妻、不封地、不生子。我将不戴宝冠,不争荣宠。我将尽忠职守,生死於斯。我是黑暗中的利剑,长城上的守卫。我是抵御寒冷的烈焰,破晓时分的光线,唤醒眠者的号角,守护王国的坚盾。我将生命与荣耀献给守夜人,今夜如此,夜夜皆然。」";
    //设置文本属性
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
    //设置行间距
    [paraStyle setLineSpacing:5];
    [paraStyle setLineBreakMode:NSLineBreakByWordWrapping];
    //字体大小
    UIFont *textFont = [UIFont systemFontOfSize:15];
    //字体颜色
    UIColor *textColor = [UIColor lightGrayColor];
    NSDictionary *textDic = @{NSFontAttributeName : textFont,NSForegroundColorAttributeName : textColor,NSParagraphStyleAttributeName : paraStyle};
    //富文本
    NSAttributedString *attrText = [[NSAttributedString alloc]initWithString:text attributes:textDic];
    //计算文本的size
    CGSize textSize = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width *0.5, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDic context:nil].size;
    
    UILabel *textLabel = [[UILabel alloc]init];
    textLabel.attributedText = attrText;
    textLabel.numberOfLines = 0;
    [self.view addSubview:textLabel];
    //布局
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
        make.width.mas_equalTo(ceil(textSize.width));//
        make.height.mas_equalTo(ceil(textSize.height));
    }];
    
    //设置虚线边框
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.bounds = CGRectMake(0, 0, textSize.width + 10, textSize.height + 10);
    //中心点位置
    borderLayer.position = CGPointMake(textSize.width *0.5, textSize.height *0.5);
    
    borderLayer.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, textSize.width + 10, textSize.height + 10)].CGPath;
    //边框的宽度
    borderLayer.lineWidth = 3;
    //边框虚线线段的宽度
    borderLayer.lineDashPattern = @[@5,@5];
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = [UIColor purpleColor].CGColor;
    [textLabel.layer addSublayer:borderLayer];

效果图

![Uploading Snip20160513_1_356370.png . . .]


Snip20160513_1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,084评论 25 709
  • 1.半透明边框 问题: 如果我们要为一个容器设置红色背景和一道黑色半透明边框,我们可能会这样写: 但是效果却是这样...
    FlyingWWS阅读 9,597评论 4 21
  • 1.多重边框 我们可以通过使用border-image来写一个多重边框,或使用多个元素来模拟多重边框,不过我们有更...
    FlyingWWS阅读 7,038评论 2 5
  • 马牛羊,鸡犬豕。此六畜,人所饲。 还是先从字面上来看,再简单不过了,就是人类驯化、饲养的六种动物,这个连解释都...
    山水国学阅读 5,704评论 0 2
  • 今天是最后一天了,我一直都没有很排斥也没有感觉很喜欢。这个军训十几天我不知道是怎么过来的。但是也并没有完全没有收获...
    不要藏在心里就好阅读 1,526评论 0 1

友情链接更多精彩内容