iOS 設置下劃線與文字之間的距離

富文本豐富了UITextView和和UILabel的展示文本的形式
設置下劃線可以使用富文本進行設置,行了,閑話少說,直接上需求和樣式吧。
需求就是:調整下下劃線與文字之間的距離,因為使用富文本本身設置的下劃線幾乎是貼著文字展示的。

image.png

沒有找到相關的方法進行設置<如果哪位大神找到的話,記得交流下>。
沒有找到相關的設置方法,只能是歪門邪道的想了。
發(fā)現(xiàn)一個簡單暴力<比較low,但是效果還不錯>的方法。
原理效果如下:

image.png

大概的思路如下:
1、創(chuàng)建兩個不管富文本,還是大小,什么屬性都一樣的UITextView或者UILabel。
2、一個只顯示字兒(textView1),一個只顯示下劃線(textView2)
3、將textView2添加到textView1上面,并且textView2.y = 你想控制的距離
說白了就是給textView1搞個影子。
相關的注意事項都寫在了注釋中。

具體代碼如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *contentStr = @"鯨屬于脊索動物門,脊椎動物亞門,哺乳綱,真獸亞綱,包含了大約98種生活在海洋、河流中的胎生哺乳動物。中國海域就有30余種?!蚌L”本身定義比較模糊,鯨目可以包含所有鯨類,還有特定科的鯨類。";
    
    // 設置下劃線的顏色為透明,或者不設置
    _textView1 = [self createTextViewWithTextColor:[UIColor blackColor] underLineColor:[UIColor clearColor] withText:contentStr];
    _textView1.editable = NO;
    [_textView1 sizeToFit];
    [self.view addSubview:_textView1];
    
    CGRect textView1Frame = _textView1.frame;
    textView1Frame.size.height = 350;
    _textView1.frame = textView1Frame;
    
    _textView1.layer.borderColor = [UIColor redColor].CGColor;
    _textView1.layer.borderWidth = 1;
    
    
    // 設置字兒的顏色為透明
    _textView2 = [self createTextViewWithTextColor:[UIColor clearColor] underLineColor:[UIColor blueColor] withText:contentStr];
    // 關閉交互,不然會影響_textView1的滾動或者相關事件的響應
    _textView2.userInteractionEnabled = NO;
    // 得設置最大高度,不然在_textView1滾動的時候顯示不全
    [_textView2 sizeToFit];
    // 背景顏色設置為透明,不然會影響_textView1的顯示
    _textView2.backgroundColor = [UIColor clearColor];
    // 一定要添加在_textView1上面,不然滾動就不能玩兒了
    [_textView1 addSubview:_textView2];
    CGRect textView2Frame = _textView2.frame;
    // 這里設置的是下劃線和字兒之間的距離
    textView2Frame.origin.y = 10;
    // 因為textView的_textView1.contentInset.left的關系,得設置偏左一些,不然會對應不上
    textView2Frame.origin.x = -_textView1.contentInset.left;
    _textView2.frame = textView2Frame;
    
}

/**
 創(chuàng)建一個textView

 @param textColor 字體的顏色
 @param underLineColor 下劃線的顏色
 @param content 顯示的內(nèi)容
 */
- (UITextView *)createTextViewWithTextColor:(UIColor *)textColor underLineColor:(UIColor *)underLineColor withText:(NSString *)content
{
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, KWidth-20, 0)];
    NSMutableAttributedString *attributedString = [self attributedStringForChat:content withColor:textColor withFontSize:[UIFont systemFontOfSize:25] withLineSpace:30 withUnderLineColor:underLineColor];
    textView.attributedText = attributedString;
    
    return textView;
}


/**
 設置富文本

 @param textString 富文本的內(nèi)容
 @param color 文本的顏色
 @param font 文字的大小
 @param lineSpace 行間距
 @param underLineColor 下劃線的顏色
 @return 富文本
 */
- (NSMutableAttributedString *)attributedStringForChat:(NSString *)textString withColor:(UIColor *)color withFontSize:(UIFont *)font withLineSpace:(CGFloat)lineSpace withUnderLineColor:(UIColor *)underLineColor{
    NSInteger strLength=textString.length;
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString:textString];
    //字顏色
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, strLength)];
    //字大小
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, strLength)];
    //行間距
    NSMutableParagraphStyle *paragraphStyle=[[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, strLength)];
    
    // 設置下劃線
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 18)];
    
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:NSMakeRange(25, 10)];
    
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(strLength-18, 10)];
    
    [attributedString addAttribute:NSUnderlineColorAttributeName value:underLineColor range:NSMakeRange(0, strLength)];
    
    
    return attributedString;
}

最終實現(xiàn)效果:


image.png

如有失誤請各位路過大神即時指點,或有更好的做法,也請指點一二。

相關的<a >Demo參考地址</a>

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容