zoukankan      html  css  js  c++  java
  • 富文本NSMutableAttributedString的相关设置

    1、NSMutableAttributedString总的来说一共有21个属性:

    NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
    NSForegroundColorAttributeNam      设置字体颜色,取值为 UIColor对象,默认值为黑色
    NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
    NSLigatureAttributeName            设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
    NSKernAttributeName                设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
    NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
    NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
    NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
    NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色
    NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
    NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor 对象
    NSShadowAttributeName              设置阴影属性,取值为 NSShadow 对象
    NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
    NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
    NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
    NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
    NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写
    NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
    NSLinkAttributeName                设置链接属性,点击后调用浏览器打开指定URL地址
    NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
    NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象

    2、在代码中的相应实现

    NSString *str = @"Hello World!";
        
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str];
        
        [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 6)];
    //    [attributeString addAttribute:NSBackgroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSUnderlineColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSLigatureAttributeName value:@1 range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSKernAttributeName value:@10 range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSStrikethroughStyleAttributeName value:@10 range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blackColor] range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 3)];
        [attributeString addAttribute:NSBaselineOffsetAttributeName value:@20 range:NSMakeRange(6, 1)];
        [attributeString addAttribute:NSVerticalGlyphFormAttributeName value:@1 range:NSMakeRange(6, 6)];
        [attributeString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://www.baidu.com"] range:NSMakeRange(6, 6)];
        
        _textview.editable = NO;
        _textview.attributedText = attributeString;

    3、运行这段代码的具体效果如图:

    4、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则不会。

    要设置代理:<UITextViewDelegate>

    同时实现代理方法:

    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
        
        return YES;
    }

    如果返回NO则text中的链接没有点击效果 。

  • 相关阅读:
    spring boot Jar
    通过JS判断设备类型
    JS获取本周、上月、本月、上月的开端日期、停止日期
    移动端长按删除事件
    获取浏览器的User Anent及判断微信浏览器
    jquery.range.js左右滑动选取数值插件,动态改变进度。
    JAVA 基础 /第九课: 变量 / JAVA中 什么是变量
    dva基本用法
    Generator 简介
    使用vuex的流程随笔
  • 原文地址:https://www.cnblogs.com/h-tao/p/5145673.html
Copyright © 2011-2022 走看看