zoukankan      html  css  js  c++  java
  • 简易使用UILabel的富文本

    简易使用UILabel的富文本

    使用效果:

    源码:

    NSString+YX.h    NSString+YX.m

    //
    //  NSString+YX.h
    //  YXKit
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "ConfigAttributedString.h"
    
    @interface NSString (YX)
    
    // 创建富文本并配置富文本(NSArray中的数据必须是ConfigAttributedString对象合集)
    - (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs;
    
    // 用于搜寻一段字符串在另外一段字符串中的NSRange值
    - (NSRange)rangeFrom:(NSString *)string;
    
    // 本字符串的range
    - (NSRange)range;
    
    @end
    //
    //  NSString+YX.m
    //  YXKit
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "NSString+YX.h"
    
    @implementation NSString (YX)
    
    - (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs
    {
        NSMutableAttributedString *attributedString = 
        [[NSMutableAttributedString alloc] initWithString:self];
        
        [configs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            ConfigAttributedString *oneConfig = obj;
            [attributedString addAttribute:oneConfig.attribute
                                     value:oneConfig.value
                                     range:oneConfig.range];
        }];
        
        return attributedString;
    }
    
    - (NSRange)rangeFrom:(NSString *)string
    {
        return [string rangeOfString:self];
    }
    
    - (NSRange)range
    {
        return NSMakeRange(0, self.length);
    }
    
    @end

    ConfigAttributedString.h   ConfigAttributedString.m

    //
    //  ConfigAttributedString.h
    //  NSMutableAttributedString
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface ConfigAttributedString : NSObject
    
    @property (nonatomic, strong, readonly) NSString *attribute; // 富文本属性
    @property (nonatomic, strong, readonly) id        value;     // 富文本值
    @property (nonatomic, assign, readonly) NSRange   range;     // 富文本范围值
    
    // 通用型配置
    + (instancetype)attribute:(NSString *)attribute
                        value:(id)value
                        range:(NSRange)range;
    
    // 配置字体
    + (instancetype)font:(UIFont *)font
                   range:(NSRange)range;
    
    // 配置字体颜色
    + (instancetype)foregroundColor:(UIColor *)color
                              range:(NSRange)range;
    
    // 配置字体背景颜色
    + (instancetype)backgroundColor:(UIColor *)color
                              range:(NSRange)range;
    
    // 字体描边颜色以及描边宽度以及阴影(以下两个方法可以一起使用)
    + (instancetype)strokeColor:(UIColor *)color
                          range:(NSRange)range;
    + (instancetype)strokeWidth:(float)number
                          range:(NSRange)range;
    + (instancetype)shadow:(NSShadow *)shadow
                     range:(NSRange)range;
    
    // 配置文字的中划线
    + (instancetype)strikethroughStyle:(NSInteger)number
                                 range:(NSRange)range;
    
    // 配置文字的下划线
    + (instancetype)underlineStyle:(NSInteger)number
                             range:(NSRange)range;
    
    // 字间距
    + (instancetype)kern:(float)number
                   range:(NSRange)range;
    
    // 段落样式(需要将UILabel中的numberOfLines设置成0才有用)
    + (instancetype)paragraphStyle:(NSMutableParagraphStyle *)style
                             range:(NSRange)range;
    
    @end
    //
    //  ConfigAttributedString.m
    //  NSMutableAttributedString
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "ConfigAttributedString.h"
    
    @interface ConfigAttributedString ()
    
    @property (nonatomic, strong) NSString *attribute;
    @property (nonatomic, strong) id        value;
    @property (nonatomic, assign) NSRange   range;
    
    @end
    
    @implementation ConfigAttributedString
    
    + (instancetype)attribute:(NSString *)attribute value:(id)value range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        
        config.attribute = attribute;
        config.value     = value;
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)font:(UIFont *)font range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSFontAttributeName;
        config.value     = font;
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)foregroundColor:(UIColor *)color range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSForegroundColorAttributeName;
        config.value     = color;
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)backgroundColor:(UIColor *)color range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSBackgroundColorAttributeName;
        config.value     = color;
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)strikethroughStyle:(NSInteger)number range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSStrikethroughStyleAttributeName;
        config.value     = [NSNumber numberWithInteger:number];
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)paragraphStyle:(NSMutableParagraphStyle *)style range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSParagraphStyleAttributeName;
        config.value     = style;
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)kern:(float)number range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSKernAttributeName;
        config.value     = [NSNumber numberWithFloat:number];
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)underlineStyle:(NSInteger)number range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSUnderlineStyleAttributeName;
        config.value     = [NSNumber numberWithInteger:number];
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)strokeColor:(UIColor *)color range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSStrokeColorAttributeName;
        config.value     = color;
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)strokeWidth:(float)number range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSStrokeWidthAttributeName;
        config.value     = [NSNumber numberWithFloat:number];
        config.range     = range;
        
        return config;
    }
    
    + (instancetype)shadow:(NSShadow *)shadow range:(NSRange)range
    {
        ConfigAttributedString *config = [self new];
        config.attribute = NSShadowAttributeName;
        config.value     = shadow;
        config.range     = range;
        
        return config;
    }
    
    @end

    控制器源码:

    //
    //  RootViewController.m
    //  RichText
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "NSString+YX.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        // 字符串
        NSString *str = @"未选择的路-弗罗斯特
    黄色的树林里分出两条路,
    可惜我不能同时去涉足,
    我在那路口久久伫立,
    我向着一条路极目望去,
    直到它消失在丛林深处。
    但我却选了另外一条路,
    它荒草萋萋,十分幽寂,
    显得更诱人、更美丽,
    虽然在这两条小路上,
    都很少留下旅人的足迹,
    虽然那天清晨落叶满地,
    两条路都未经脚印污染。
    啊,留下一条路等改日再见!
    但我知道路径延绵无尽头,
    恐怕我难以再回返。
    也许多少年后在某个地方,
    我将轻声叹息把往事回顾,
    一片树林里分出两条路,
    而我选了人迹更少的一条,
    从此决定了我一生的道路。";
        
        // 设置组
        NSArray *array = 
        @[// 全局设置
          [ConfigAttributedString font:[UIFont systemFontOfSize:10.f] range:[str range]],
          [ConfigAttributedString paragraphStyle:[self style]         range:[str range]],
          
          // 局部设置
          [ConfigAttributedString foregroundColor:[UIColor redColor]
                                            range:[@"未选择的路" rangeFrom:str]],
          [ConfigAttributedString font:[UIFont systemFontOfSize:20.f]
                                 range:[@"未选择的路" rangeFrom:str]]];
        
        // 初始化富文本
        UILabel *label       = [[UILabel alloc] initWithFrame:self.view.bounds];
        label.numberOfLines  = 0;
        label.attributedText = [str createAttributedStringAndConfig:array];
        [self.view addSubview:label];
    }
    
    // 段落样式
    - (NSMutableParagraphStyle *)style
    {
        NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
        style.lineSpacing              = 10.f;
        style.firstLineHeadIndent      = 10.f;
        
        return style;
    }
    
    @end

    设计原理:

    我把配置抽象成了一个对象,一个对象可能对应着不同的配置,这样写起来就不会乱

    使用的时候直接就在数组中配置

    通过NSString的Category直接生成富文本赋值给attributedText即完成了富文本的操作

    这才叫面向对象编程嘛:)

  • 相关阅读:
    linux部署tomcat服务器
    如何设计功能测试
    sql语句字符串型日期转化为数字类型
    关于软件测试的基础知识
    关于数据库的一些基本知识
    py,先导,--L
    selenium,常用网站
    maven,使用
    移动自动化,appium,java--L
    接口,自动化,java--L
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3875542.html
Copyright © 2011-2022 走看看