富文本簡(jiǎn)單設(shè)置、加虛線邊框

    NSString *text = @"守夜人誓言:「長(zhǎng)夜將至,我從今開始守望,至死方休。我將不娶妻、不封地、不生子。我將不戴寶冠,不爭(zhēng)榮寵。我將盡忠職守,生死於斯。我是黑暗中的利劍,長(zhǎng)城上的守衛(wèi)。我是抵御寒冷的烈焰,破曉時(shí)分的光線,喚醒眠者的號(hào)角,守護(hù)王國(guó)的堅(jiān)盾。我將生命與榮耀獻(xiàn)給守夜人,今夜如此,夜夜皆然。」";
    //設(shè)置文本屬性
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
    //設(shè)置行間距
    [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];
    //計(jì)算文本的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));
    }];
    
    //設(shè)置虛線邊框
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.bounds = CGRectMake(0, 0, textSize.width + 10, textSize.height + 10);
    //中心點(diǎn)位置
    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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,841評(píng)論 25 708
  • 1.半透明邊框 問(wèn)題: 如果我們要為一個(gè)容器設(shè)置紅色背景和一道黑色半透明邊框,我們可能會(huì)這樣寫: 但是效果卻是這樣...
    FlyingWWS閱讀 4,123評(píng)論 4 21
  • 1.多重邊框 我們可以通過(guò)使用border-image來(lái)寫一個(gè)多重邊框,或使用多個(gè)元素來(lái)模擬多重邊框,不過(guò)我們有更...
    FlyingWWS閱讀 2,428評(píng)論 2 5
  • 馬牛羊,雞犬豕。此六畜,人所飼。 還是先從字面上來(lái)看,再簡(jiǎn)單不過(guò)了,就是人類馴化、飼養(yǎng)的六種動(dòng)物,這個(gè)連解釋都...
    山水國(guó)學(xué)閱讀 1,591評(píng)論 0 2
  • 今天是最后一天了,我一直都沒(méi)有很排斥也沒(méi)有感覺(jué)很喜歡。這個(gè)軍訓(xùn)十幾天我不知道是怎么過(guò)來(lái)的。但是也并沒(méi)有完全沒(méi)有收獲...
    不要藏在心里就好閱讀 205評(píng)論 0 1