zoukankan      html  css  js  c++  java
  • swift 截取字符串

    swift3.0 使用String.Index来做位置索引,要想实现截取功能首先得获取到String.Index;

    获取中间部分参数用Range<Index>即可;

    let str:String = btn.currentTitle ?? ""

             let indes = str.index(str.startIndex, offsetBy: str.length - 1)

             let prefix = str.substring(to: indes)

             btn.setTitle(prefix + "▾", for: .normal)

    举例从开始截取到倒数第二位。

    let index = str.index(str.endIndex, offsetBy: -5)
            
        let suffix = str.substring(from: index)

    截取倒数五位

    2.富文本设置 (空格也会占位)

          guard let mingzi = model.name,let jieshao = model.bio else {

             return

          }

          let amountString = NSMutableAttributedString(string:"(mingzi)/(jieshao)")

          //设置字体颜色

          amountString.addAttribute(NSForegroundColorAttributeName, value: UIColor.black,range: NSMakeRange(0, "(mingzi)".length + 1))

          amountString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13),range: NSMakeRange(0, "(mingzi)".length + 1))

          

          amountString.addAttribute(NSForegroundColorAttributeName, value: UIColor.gray,range: NSMakeRange(1 + "(mingzi)".length, "(jieshao)".length))

          amountString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13),range: NSMakeRange(1 + "(mingzi)".length, "(jieshao)".length))

          self.bio.attributedText = amountString

  • 相关阅读:
    成功熬了四年还没死?一个IT屌丝创业者的深刻反思
    史氏语录
    WEB安全攻防学习内容
    从程序员的角度谈创业三年
    Windows2008 R2修改3389端口教程
    Win2008R2 zip格式mysql 安装与配置
    制作支持UEFI PC的Server2008 R2系统安装U盘
    郎科U208(主控 PS2251-50 HYNIX H27UCG8T2MYR)量产还原
    自用有线IP切换
    自动配置IP地址.bat
  • 原文地址:https://www.cnblogs.com/linximu/p/6699762.html
Copyright © 2011-2022 走看看