iOS-富文本

富文本是什么?

普通的文本展示就是label.text = @"xxxxxx",只能設(shè)置顏色、大小、背景色等等。
但是在實(shí)際開發(fā)中,我們需要用到圖片/表情和文字的混合排版,那么就需要用到富文本了。
OC中提供了一個富文本類,NSAttributedString。有了他之后我們就可以用它來實(shí)現(xiàn)在UILabel、UIButton、UITextView上的圖文混排效果,需要用到這些空間的attributedText屬性。

在UILabel中

- (void)richText1
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 40)];
    [self.view addSubview:label];
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"愛上帝啊商店大的撒"];
    
    //給指定范圍的文字添加屬性
    //[string addAttributes:@{NSFontAttributeName:} range:<#(NSRange)#>]
    
    //附件
    NSTextAttachment *attach1 = [[NSTextAttachment alloc] init];
    //調(diào)整圖片位置
    attach1.bounds = CGRectMake(0, 0, 20, 20);
    //設(shè)置圖片
    attach1.image = [UIImage imageNamed:@"001"];
    
    [string appendAttributedString:[NSAttributedString attributedStringWithAttachment:attach1]];
    
    label.attributedText = string;
}

效果如圖:


1.png

在UITextView中

在UITextView中還可以識別電話、網(wǎng)址、地址等等

#pragma mark - textView的富文本
- (void)richText2
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"fsaada http://www.baidu.com 10086 13888888888 dawa"];
    
    //多行輸入框
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 40, 300, 300)];
    
    textView.backgroundColor = [UIColor lightGrayColor];
    
    //禁用編輯
    textView.editable = NO;
    
    textView.delegate = self;
    //啟動自動識別電話號碼或網(wǎng)址、郵箱
    textView.dataDetectorTypes = UIDataDetectorTypeAll;
    
    [self.view addSubview:textView];
    
    //附件
    NSTextAttachment *attach1 = [[NSTextAttachment alloc] init];
    //調(diào)整圖片位置
    attach1.bounds = CGRectMake(0, 0, 20, 20);
    //設(shè)置圖片
    attach1.image = [UIImage imageNamed:@"001"];
    
    //添加圖片
    [attributeString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attach1]];
    
    //添加一個鏈接
    NSString *url = @"進(jìn)入百度";
    
    NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:url];
    //添加屬性
    [linkString addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(0, url.length)];
    
    [attributeString appendAttributedString:linkString];
    
    textView.attributedText = attributeString;
}

UITextView的代理方法

#pragma mark - UITextViewDelegate
//點(diǎn)擊url會觸發(fā)
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    //NSString *urlString = URL.absoluteString;
    
    //可以跳轉(zhuǎn)到瀏覽器
    return YES;
}

//點(diǎn)擊添加到輸入框的NSTextAttachment對象會觸發(fā)
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
    //獲取點(diǎn)擊的圖片
    //UIImage *image = textAttachment.image;
    
    //返回NO,系統(tǒng)actionSheet不會彈出
    return NO;
}
2.png
3.png

YYText

!支持高性能的異步排版和渲染
!擴(kuò)展了 CoreText 的屬性以支持更多文字效果
!支持 UIImage、UIView、CALayer 作為圖文混排元素

詳情:
YYText-GitHub

- (void)YYText
{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"12dk990k09edq啊實(shí)打?qū)嵈舐暤腊采綎|安山東安山東聲道安山東安山東安山東聲道安山東安山東安山東聲道安山東安山東安山東聲道安山東安山東安山東聲道安山東安山東安山東聲道安山東安山東安山東"];
    //string.yy_color = [UIColor redColor];
    [string yy_setColor:[UIColor redColor] range:NSMakeRange(0, 2)];
    string.yy_lineSpacing = 10;
    [string yy_setTextHighlightRange:NSMakeRange(3, 10) color:[UIColor cyanColor] backgroundColor:[UIColor orangeColor] userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        //
    } longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //
    }];
    
    //創(chuàng)建一個
    
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"衛(wèi)星地圖",@"普通地圖"]];
    
    NSMutableAttributedString *attributeS1 = [NSMutableAttributedString yy_attachmentStringWithContent:segment contentMode:UIViewContentModeScaleAspectFill width:200 ascent:10 descent:10];
    
    [string appendAttributedString:attributeS1];
    
    YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(20, 40, 350, 400)];
    
    label.backgroundColor = [UIColor lightGrayColor];
    label.numberOfLines = 0;
    label.attributedText = string;
    
    [self.view addSubview:label];
}
1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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