zoukankan      html  css  js  c++  java
  • iOS富文本(一)属性化字符串

    概述

    iOS一些复杂的文本布局一般都是由底层的Core Text来实现的,直到iOS7苹果发布了Text Kit框架,Text Kit能够很简单实现一些复杂的文本样式以及布局,而Text Kit富文本框架用到的核心数据结构就是属性化字符串NSAttributeString,本篇文章将介绍NSAttributeString一些常用属性和使用方法。


    字体样式

    NSAttributeString有很多属性提供来改变字体的样式,下面代码只是列出了一些常用的属性,如果想更改更多的字体样式请参考苹果官方文档,使用方法大同小异。

    代码示例

    @interface ViewController ()
    @property (weak,nonatomic) IBOutlet UILabel *stringLabel;
    @end
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *string = _stringLabel.text;
        //初始化属性字符串
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
        //字体类型属性
        NSDictionary *BoldFontAS = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
        [attributedString addAttributes:BoldFontAS range:[string rangeOfString:@"BoldFont"]];
    
        //字体颜色属性
        NSDictionary *RedFondAS = @{NSForegroundColorAttributeName :[UIColor redColor]};
        [attributedString addAttributes:RedFondAS range:[string rangeOfString:@"RedFont"]];
    
         //字体背景颜色和字体颜色属性
        NSDictionary *BuleBackgroundAS = @{NSBackgroundColorAttributeName:[UIColor blueColor],  NSForegroundColorAttributeName:[UIColor whiteColor]};
        [attributedString addAttributes:BuleBackgroundAS range:[string rangeOfString:@"BuleBackground"]];
    
        //字体下划线与字体下划线颜色属性
        NSDictionary *UnderlineAS = @{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName:[UIColor greenColor]};
        [attributedString addAttributes:UnderlineAS range:[string rangeOfString:@"Underline"]];
    
        //字体阴影属性
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowOffset = CGSizeMake(2, 2);
        shadow.shadowColor = [UIColor orangeColor];
        NSDictionary *ShadowAS = @{NSShadowAttributeName:shadow};
        [attributedString addAttributes:ShadowAS range:[string rangeOfString:@"Shadow"]];
        //设置Label的字符串属性
        _stringLabel.attributedText = attributedString;
    
    }

    实现效果


    段落样式

    NSAttributeString的段落样式包括外边距,字体对齐,字体缩进等。在iOS中段落用 用来分隔,如果在一个段落中使用多个段落样式的话,那么只对段落中第一个字符使用的样式有效。在一段文字中如果没有 的话那么这一段文字就是一个段落。在显示中常常文字过长系统会自动换行,不管换多少行都只是一个段落。

    对整体的文本应用段落样式

    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UILabel *paragraphLabel;
    @end
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString * paragraphString = @"An NSAttributedString object manages character strings
    and associated sets of attributes (for example, font and kerning)
    that apply to individual characters or ranges of characters in the string.";
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        [paragraph setLineSpacing:8];   //对每一个段落设置行间距
        [paragraph setParagraphSpacing:15]; //设置段落之间的间距
        [paragraph setFirstLineHeadIndent:20];  //设置每个段落的首行缩进
        //初始化段落属性
        NSDictionary *attribute = @{NSParagraphStyleAttributeName:paragraph};
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:paragraphString attributes:attribute];
        _paragraphLabel.attributedText = attributeString;
    }

    设置段落前

    设置段落后

  • 相关阅读:
    关于Dll、Com组件、托管dll和非托管dll的理解
    委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件
    类静态和实例化执行顺序优先级(静态构造函数、静态变量、静态方法)
    ActionFilter的四个方法使用场景
    C# 中? 和 ?? 在变量中的使用
    Nginx 安装配置
    jetty + maven + (frontend) + eclipse
    plsql function
    [Postgres] drop database , but the database is being accessed by other users
    js在table中添加tbody块,方便整块的添加和删除
  • 原文地址:https://www.cnblogs.com/itrena/p/5927105.html
Copyright © 2011-2022 走看看