YYText/TTTAttributed

作者: wpf_register | 来源:发表于2016-08-11 11:31 被阅读1237次

参考文档1
参考文档2
[参考文档3]( [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:[UIColor redColor]
range:fontRange];)
之前写过关于富文本的处理,确实略显麻烦文章链接,相比而言,YYText使用简单且功能强大。当然汲及到富文本链接,多用TTTAttributedLabel。具体项目中MLEmojiLabel涉及表情处理的话感觉更方便一些。

YYLabel 设置字体样式更方便简洁
TTT 及其子类 ML 设置字体样式等感觉代码有点烦琐,处理链接方便,且可以很方便准确的处理给定文字的高度处理。

YYText

  • 基础用法
#import "ViewController.h"
#import <YYText.h>

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
#pragma mark - 基本用法
    //基本用法和UILabel相似
    YYLabel *label = [[YYLabel alloc]init];
    label.frame = CGRectMake(0, 0, 200, 200);
    label.center = self.view.center;
    label.numberOfLines = 2;
    [self.view addSubview:label];
    
#pragma mark - 属性文本
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"Change the fucking world"];
    str.yy_font = [UIFont systemFontOfSize:20];
    str.yy_color = [UIColor orangeColor];
    [str yy_setColor:[UIColor greenColor] range:NSMakeRange(7, 4)];

     //*******也可以直接搜索相应文字改变属性******//
     //NSRange range= [[text string] rangeOfString:@"对这个世界如果你有太多的抱怨" options:NSCaseInsensitiveSearch];

    //行间距
    str.yy_lineSpacing =10;


    //文字描边(空心字)默认黑色,必须设置width
    [text yy_setStrokeColor:[UIColor orangeColor] ];
    [text yy_setStrokeWidth:@(2) ];


     YYTextDecoration *decoration = 
            [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle
                                            width:@(1)
                                            color:[UIColor blueColor]];
    //删除样式
    [text yy_setTextStrikethrough:decoration range:range2];
    //下划线
    [text yy_setTextUnderline:decoration range:range2];


    //阴影
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowColor = [UIColor grayColor];
    shadow.shadowOffset = CGSizeMake(1, 1.5);
    str.yy_shadow  = shadow;
    
    //文本内阴影
    YYTextShadow *shadow = [YYTextShadow new];
    shadow.color = [UIColor redColor];
    shadow.offset = CGSizeMake(0, 2);
    shadow.radius = 1;
    [str yy_setTextInnerShadow:shadow ];


    //首行缩进
    str.yy_firstLineHeadIndent = 10;

    //背景框
    //多行显示时效果会不太如愿          
    str.yy_textBorder = [ YYTextBorder borderWithLineStyle:YYTextLineStylePatternCircleDot
                                                 lineWidth:1
                                               strokeColor:[UIColor greenColor]];
    str.yy_textBorder.cornerRadius = 3;
    str.yy_textBorder.insets = UIEdgeInsetsMake(0, -2, 0, -2);
    

  #pragma mark - 高亮显示
    
   [str yy_setTextHighlightRange:range
                         color:[UIColorr redColor]
               backgroundColor:[UIColor greenColor]
                     tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                        //点击事件
                     }];
    
    //一定要放在最后
    label.attributedText = str;


}

  • YYTextRubyAnnotation
 NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"这是用汉语写的一段文字。"];
    one.yy_font = [UIFont boldSystemFontOfSize:30];

    YYTextRubyAnnotation *ruby;
    ruby = [YYTextRubyAnnotation new];
    ruby.textBefore = @"hàn yŭ";
    [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"汉语"]];
    
    ruby = [YYTextRubyAnnotation new];
    ruby.textBefore = @"wén";
    [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"文"]];
    
    ruby = [YYTextRubyAnnotation new];
    ruby.textBefore = @"zì";
    ruby.alignment = kCTRubyAlignmentCenter;
    [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"字"]];
    
    YYLabel *label = [YYLabel new];
    label.backgroundColor = [UIColor whiteColor];
    label.attributedText = one;
    label.frame = CGRectMake(0, 10, self.view.frame.size.width, 200);
    label.textAlignment = NSTextAlignmentCenter;
    label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    label.numberOfLines = 1;
    [self.view addSubview:label];

TTTAttributed

 -(void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.label];
    
    //也可以是NSString字符串
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"注意注意注意注意注意注意"];
   
    __block CGSize size;
    
    [self.label setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
       
        
        //文字边框颜色
        NSRange range ;
        [mutableAttributedString addAttribute:kTTTBackgroundStrokeColorAttributeName
                                        value:[UIColor purpleColor]
                                        range:range];
        
        //删除样式
        [mutableAttributedString addAttribute:kTTTStrikeOutAttributeName
                                        value:@YES
                                        range:range];
        
        [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
                                        value:[UIColor redColor]
                                        range:range];
        
        //高度计算
        size = [TTTAttributedLabel sizeThatFitsAttributedString:mutableAttributedString
                                                withConstraints:CGSizeMake(kWidth, CGFLOAT_MAX)
                                         limitedToNumberOfLines:0];
        return mutableAttributedString;
        
        
    }];
    
    
    NSRange boldRange1 = [str rangeOfString:@"测试" options:NSCaseInsensitiveSearch];
    [self.label addLinkToURL:[NSURL URLWithString:@"http://www.taobao.com"]
                   withRange:boldRange1];
    
    //电话号码可以自动识别,也可以这样添加
    //NSRange phoneRange = [text rangeOfString:phoneNum options:NSCaseInsensitiveSearch];
    //[self.label addLinkToPhoneNumber:phoneNum withRange:phoneRange];
    
    self.label.frame = CGRectMake(0, 30, kWidth, size.height);
    
    
}

- (TTTAttributedLabel*)label{
    
    if (!_label) {
        _label = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
        
        _label.lineBreakMode = NSLineBreakByTruncatingHead;
        _label.numberOfLines = 0;
        //_label.delegate = self;
        _label.lineSpacing = 10;
        
        /*
         要放在`text`,
        `setText:`
        `setText:afterInheritingLabelAttributesAndConfiguringWithBlock:前面才有效
         */
        
        _label.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress|NSTextCheckingTypeLink;
        //链接正常状态文本属性
        _label.linkAttributes = @{NSForegroundColorAttributeName:[UIColor purpleColor],NSUnderlineStyleAttributeName:@(1)};
        //链接高亮状态文本属性
        _label.activeLinkAttributes = @{NSForegroundColorAttributeName:[UIColor redColor],NSUnderlineStyleAttributeName:@(1)};
        
    }
    return _label;
    
}

空心字
文本内阴影
文本阴影
背景框

相关文章

  • YYText/TTTAttributed

    参考文档1参考文档2[参考文档3]( [mutableAttributedString addAttribute:...

  • YYText源码分析

    YYText 简单介绍 YYText 是YYKit中的一个富文本显示,编辑组件,拥有YYLabel,YYText...

  • YYKit之YYText、YYLabel使用总结(更新)

    1.索引 YYText YYLabel 更新 YYText IQKeyboardManager支持 布局问题 如果...

  • YYText使用

    NSMutableString + YYText实例YYText+add tag制作文字tag 文字附加attac...

  • YYText 源码解析

    YYText [%5Bhttps://github.com/ibireme/YYText%5D(https://g...

  • YYText使用

    YYText官方文档[https://github.com/ibireme/YYText](官方文档写的也很清楚,...

  • YYtext

    http://www.jianshu.com/p/60aee32ade55?nomobile=yes

  • YYText

    1.YYKit之YYText2.CoreText实现图文混排和点击事件3.认识 TextKit4.CoreText...

  • YYText

    1.里面主要两个控件: YYTextView 和 YYLabel 现在主要是YYTextview的简单使用 YYT...

  • YYText源码阅读(一):YYLabel

    YYText介绍 YYText 功能强大的 iOS 富文本编辑与显示框架,是 YYKit 的组件之一。在此感谢作者...

网友评论

  • 指尖猿:text,跟上面的str对不上...专业?
  • 0f815988b64f:做高亮文本点击是不是用拖拽控件无法实现点击效果?yylabel创建的label可以
    wpf_register:@Doze 具体我没尝试过,也许可以,但会很麻烦吧。yyLabel就很方便
  • 405b454dbb8d:老司机

本文标题:YYText/TTTAttributed

本文链接:https://www.haomeiwen.com/subject/kmtssttx.html