【问题标题】:Change the colour of the bullets in UITextView更改 UITextView 中项目符号的颜色
【发布时间】:2015-12-29 06:29:15
【问题描述】:

我正在使用UITextView 来显示从 API 调用中获得的文本。从文本中,我需要识别特殊字符“,”(逗号)并将其替换为换行符转义序列和绿色项目符号。

我使用.stringByReplacingOccurrencesOfString(",", withString: "\n•") 完成了它。子弹来自Edit-> Emoji&Symbols。它运作良好。

但我不知道如何更改子弹的颜色。可以改变子弹的颜色吗?如果是这样,怎么做?

颜色代码是0x53B0A2

我正在使用Xcode 7.1.1,Swift 2.0。

【问题讨论】:

  • NSAttributedString
  • 我没用过,有什么使用方法吗?

标签: ios cocoa-touch uitextview special-characters nsattributedstring


【解决方案1】:

你可以使用这个功能:

func attributedTextForString(text:String)->NSAttributedString{
    let r = text.stringByReplacingOccurrencesOfString(",", withString: "\n•") as NSString

    let attributedString = NSMutableAttributedString(string: r as String)

    let greenColorAttribure = [NSForegroundColorAttributeName: UIColor(red: 83/255.0, green: 176/255.0, blue: 162/255.0, alpha: 1.0)]

    do {

        let regex = try NSRegularExpression(pattern: "•", options: NSRegularExpressionOptions.CaseInsensitive)

        regex.enumerateMatchesInString(r as String, options: [], range: NSMakeRange(0, r.length), usingBlock: { (result, flags, pointer) -> Void in

            if let result = result{
                attributedString.addAttributes(greenColorAttribure, range:result.range)
            }
        })
        return attributedString

    }catch{
        return attributedString

    }
}

只需传递字符串,它就会返回属性字符串:

        let yourText = "hekkli sdfhos afs , sdfsf sfsfjms , sdfsf skldf, kshfg "
        let coloredBulletString = attributedTextForString(yourText)
        textView.attributedText = coloredBulletString

【讨论】:

    【解决方案2】:

    在 Swift 3.1 中

      let  DelevieryDateString = "●" + "This is a list item!"
    
        let DelevieryDateStr: NSMutableAttributedString =  NSMutableAttributedString(string: DelevieryDateString)
    
        DelevieryDateStr.addAttribute(NSForegroundColorAttributeName,
                                        value: UIColor.green, //color code what you want
                                        range: NSRange(
                                            location:0, // Find the location of the bullet and replace it
                                            length: 1))
    lblitem.attributedText = DelevieryDateStr
    

    【讨论】:

      【解决方案3】:

      使用 Hamza Ansari 的答案并为 Swift 4.2 制作了它:

      extension UITextView {
          func setBulletList(text:String, bullet: String, bulletColor: UIColor, attributes: [NSAttributedString.Key: Any]) {
              let r0 = text.replacingOccurrences(of: ",", with: "\n" + bullet)
              let r = bullet + r0
      
              let attributedString = NSMutableAttributedString(string: r)
      
              attributedString.addAttributes(attributes, range: NSMakeRange(0, attributedString.length))
      
              let greenColorAttribure = [NSAttributedString.Key.foregroundColor: bulletColor]
      
              do {
      
                  let regex = try NSRegularExpression(pattern: bullet, options: NSRegularExpression.Options.caseInsensitive)
      
                  regex.enumerateMatches(in: r as String, options: [], range: NSMakeRange(0, r.count), using: { (result, flags, pointer) -> Void in
      
                      if let result = result{
                          attributedString.addAttributes(greenColorAttribure, range:result.range)
                      }
                  })
      
                  self.attributedText = attributedString
      
              } catch {
                  self.attributedText = attributedString
              }
          }
      }
      

      用法:

      lazy var textView: UITextView = {
              let view = UITextViewFixed()
      
              view.setBulletList(text: "Tags,Users,Jobs", bullet: "●  ", bulletColor: UIColor.red, attributes: [
                  NSAttributedString.Key.foregroundColor: UIColor.black,
                  NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17)
                  ])
      
              return view
          }()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-11
        • 2016-07-13
        • 1970-01-01
        • 2012-07-21
        • 2014-11-01
        • 2016-03-17
        • 1970-01-01
        相关资源
        最近更新 更多