美文网首页
NSAttributeString富文本篇

NSAttributeString富文本篇

作者: BigBossZhu | 来源:发表于2017-03-21 18:05 被阅读366次

主要类: NSAttributedString 和 NSMutableAttributedString

** 需要用到的了NSTextAttachment:文本附件,有参考网上代码 **
用法:

  1. 富文本类有点类似于字符串但是他可以实现比字符串更强大的功能,在字符串上给颜色,大小,下划线.介于lable和NSString之间
  2. 可以添加图片附件(NSTextAttachment)微博的emoji表情和文字混搭.(YYKit框架中的lable类)
  3. 实现超链接.(副文本实现超链接,只能通过UITextView实现.

代码1:常见用法

这里仅仅介绍一些有用的键,很多没用的不介绍了.
方式一:
//    方式-(逐条加入键值对,不太好记)
    NSString *str = @"人生若只如初见,何事悲风秋画扇。\n等闲变却故人心,却道故人心易变。\n骊山语罢清宵半,泪雨霖铃终不怨。\n何如薄幸锦衣郎,比翼连枝当日愿。";
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str];
    //    rang表示从哪里开始多长
    //设置字体和设置字体的范围
    [aStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0f] range:NSMakeRange(0, 3)];
    //添加文字颜色
    [aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(17, 7)];
    //添加文字背景颜色
    [aStr addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(17, 7)];
    //添加下划线
    [aStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(8, 7)];



方式二:(一起添加)
NSString *str1 = @"设置字体格式和大小";
    NSDictionary *dict1 = @{
                            //字体大小
                            NSFontAttributeName :[UIFont systemFontOfSize:33.0f],
                            //前端颜色,显示颜色
                            NSForegroundColorAttributeName : [UIColor redColor],
                            NSBackgroundColorAttributeName : [UIColor orangeColor],
                            //下划线功能,但是没有办法调整距离.需要手动增加控件
                            NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle],
                            //基线(没什么用).只是一个位置标示,字体对齐和表情的时候好像需要使用
                            NSBaselineOffsetAttributeName:@(5)
                            };
    NSMutableAttributedString *aStr1 = [[NSMutableAttributedString alloc]initWithString:str1 attributes:dict1];

 /*!
     注:NSKernAttributeName用来设置字符之间的间距,取值为NSNumber对象(整数),负值间距变窄,正值间距变宽
     */
    NSString *str4 = @"\n设置字符间距";
    NSDictionary *dictAttr4 = @{NSKernAttributeName:@(4)};
    NSAttributedString *attr4 = [[NSAttributedString alloc]initWithString:str4 attributes:dictAttr4];
    [aStr1 appendAttributedString:attr4];

/*!
     注:NSStrikethroughStyleAttributeName设置删除线,取值为NSNumber对象,枚举NSUnderlineStyle中的值。NSStrikethroughColorAttributeName设置删除线的颜色。并可以将Style和Pattern相互 取与 获取不同的效果
     */
    
    NSString *str51 = @"\n设置删除线为细单实线,颜色为红色";
    NSDictionary *dictAttr51 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName:[UIColor redColor]};
    NSAttributedString *attr51 = [[NSAttributedString alloc]initWithString:str51 attributes:dictAttr51];
    [aStr1 appendAttributedString:attr51];


代码2:添加图片附件

//设置文本附件NSAttachmentAttributeName,取值为NSTextAttachment对象,常用于文字的图文混排
    NSString *str9 = @"文字的图文混排\n";
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];
    textAttachment.image = [UIImage imageNamed:@"搜索"];
    textAttachment.bounds = CGRectMake(0, 0, 30, 30);
    NSDictionary *dictAttr9 = @{NSAttachmentAttributeName:textAttachment};
    NSAttributedString *attr9 = [[NSAttributedString alloc]initWithString:str9 attributes:dictAttr9];
//添加图片附件
    [aStr1 appendAttributedString:attr9];
//图片附件还可以插入
[aStr1 insertAttributedString: attr9 atIndex:range.location];

//存在两个问题1.图片大小不对,2图片和文字不对齐不居中.
//1. 继承NSTextAttachment重写这个方法,可以使大小相同lineFrag代表本的lable一行的宽高,都取高度才正确
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex {
    return CGRectMake(0, 0, lineFrag.size.height, lineFrag.size.height);
}
//2. 通过基线处理

代码3: 超链接:NSLinkAttributeName属性,但是使用这个有几点注意

之所以把 NSLinkAttributeName 属性单独列出来,是因为在 UILabel 和 UITextField 中是无法使用该属性的。更准确点说是在UILabel 和 UITextField 中无法实现点击链接启动浏览器打开一个URL地址,因为在此过程中用到了一个代理函数。只能用在 UITextView 中。
NSLinkAttributeName 的对象是 NSURL 类型 或 NSString,但是优先使用 NSURL。
需要实现UITextView的代理方法 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange,在该方法中,返回 YES,则会打开URL地址,返回 NO则不会.


- (void)test4 {
    NSDictionary *dict = @{
                           // NSURL (preferred) or NSString
                           NSLinkAttributeName : [NSURL URLWithString:@"http://www.baidu.com"]
                           };
    NSAttributedString *aStr = [[NSAttributedString alloc]initWithString:@"百度" attributes:dict];
//    self.lable.attributedText = aStr;
    self.textView.attributedText = aStr;
    //可编辑就不可点击
    self.textView.editable = NO;
    self.textView.delegate = self;
}
#pragma mark - UITextViewDelegate 是否能够连接互联网
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    return YES;
}

列出所有能用到的属性:

NSFontAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSUnderlineStyleAttributeName, NSBaselineOffsetAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSAttachmentAttributeName, NSLinkAttributeName

相关文章

网友评论

      本文标题:NSAttributeString富文本篇

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