zoukankan      html  css  js  c++  java
  • swift版的StringAttribute

    swift版的StringAttribute

    效果

    源码

    https://github.com/YouXianMing/Swift-StringAttribute

    //
    //  StringAttributeProtocol.swift
    //  Swift-StringAttribute
    //
    //  Created by YouXianMing on 15/10/8.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    import Foundation
    
    @objc protocol StringAttributeProtocol {
    
        /**
        富文本属性名字
        
        - returns: 属性名字
        */
        func attributeName() -> NSString;
        
        /**
        属性对应的值
        
        - returns: 对应的值
        */
        func attributeValue()-> AnyObject;
        
        /**
        属性设置生效范围
        
        - returns: 生效范围
        */
        optional func effectiveStringRange() -> NSRange;
    }
    //
    //  StringAttribute.swift
    //  Swift-StringAttribute
    //
    //  Created by YouXianMing on 15/10/8.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    import UIKit
    
    class StringAttribute: NSObject, StringAttributeProtocol {
    
        // MARK: 公用的属性
        
        /// 富文本生效范围
        var effectRange : NSRange! = NSMakeRange(0, 0)
        
        // MARK: 公用的方法
        
        /**
        属性有效范围
        
        - returns: 有效范围
        */
        func effectiveStringRange() -> NSRange {
        
            return effectRange
        }
        
        // MARK: ==由子类重写的方法==
        
        /**
        /////////////////
            由子类重写
        /////////////////
        
        属性名字
        
        - returns: 属性名字
        */
        func attributeName() -> NSString {
            
            fatalError("must be overwrote by subclass")
        }
        
        /**
        /////////////////
            由子类重写
        /////////////////
        
        属性值
        
        - returns: 属性值
        */
        func attributeValue()-> AnyObject {
            
            fatalError("must be overwrote by subclass")
        }
    }
    //
    //  NSMutableAttributedString+StringAttribute.swift
    //  Swift-StringAttribute
    //
    //  Created by YouXianMing on 15/10/8.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    import Foundation
    
    extension NSMutableAttributedString {
    
        /**
        添加富文本对象
        
        - parameter stringAttribute: 实现了StringAttributeProtocol协议的对象
        */
        func addStringAttribute(stringAttribute : StringAttributeProtocol) {
        
            self.addAttribute(stringAttribute.attributeName() as String,
                value: stringAttribute.attributeValue(),
                range: stringAttribute.effectiveStringRange!())
        }
        
        /**
        消除指定的富文本对象
        
        - parameter stringAttribute: 实现了StringAttributeProtocol协议的对象
        */
        func removeStringAttribute(stringAttribute : StringAttributeProtocol) {
        
            self.removeAttribute(stringAttribute.attributeName() as String,
                range: stringAttribute.effectiveStringRange!())
        }
    }

    分析

  • 相关阅读:
    nginx 状态码整理
    nginx 添加perl
    Nginx 内置全局变量
    数组模板实现(新手遇到的格式问题)
    malloc的使用注意事项
    使用memset的注意事项!!!
    2019/3/31acm周三(三人)/CodeForces
    2019/3/31acm周三(三人)/LightOJ
    2019/3/31acm周三(三人)/hdu1042/高精度计算(未懂!)
    2019/3/24周赛坑题(读题)HDU 1412
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4862531.html
Copyright © 2011-2022 走看看