• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
飞鸿518
博客园    首页    新随笔    联系   管理    订阅  订阅

Swift3 富文本 变更颜色 加删除线 字体大小 颜色 样式

//NSMutableAttributedString

    func Add_lineoffset(rest:String,id :String, englishName :String, chineseName :String, isdelete :Bool) -> NSMutableAttributedString {

        let attribute : NSMutableAttributedString = NSMutableAttributedString()

        let currentPriceString : NSAttributedString = NSAttributedString(string: rest, attributes: [ NSAttributedStringKey.backgroundColor : UIColor.clear,NSAttributedStringKey.foregroundColor : REJEColor, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 13.0)])

        let oldPr: NSMutableAttributedString = NSMutableAttributedString(string: id, attributes: [NSAttributedStringKey.foregroundColor: REJEColor, NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 15.0)])

        let Engstr: NSMutableAttributedString = NSMutableAttributedString(string: englishName, attributes: [NSAttributedStringKey.foregroundColor : REJEColor, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 15)])

        let chinese: NSMutableAttributedString = NSMutableAttributedString(string: chineseName, attributes: [NSAttributedStringKey.foregroundColor : REJEColor, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 15)])

        if isdelete == true {//加删除线

            oldPr.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: oldPr.length)) 

            Engstr.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: 1), range: NSMakeRange(0, Engstr.length))

            chinese.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: 1), range: NSMakeRange(0, chineseName.count))

        }

       attribute.append(currentPriceString)

        attribute.append(oldPr)

        attribute.append(Engstr)

        attribute.append(chinese)

        return attribute

    }

posted @ 2018-04-02 12:31  飞鸿518  阅读(1358)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3