Ios 更改UITextView中项目符号的颜色
我正在使用Ios 更改UITextView中项目符号的颜色,ios,cocoa-touch,uitextview,special-characters,nsattributedstring,Ios,Cocoa Touch,Uitextview,Special Characters,Nsattributedstring,我正在使用UITextView显示从API调用中获取的文本。从文本中,我需要识别特殊字符“,”(逗号),并将其替换为换行符转义序列和绿色的项目符号 我使用.stringByReplacingOccurrencesOfString(“,”和字符串:“\n•”完成了这项工作。项目符号可从Edit->Emoji&Symbols获取。它工作得很好 但我不知道如何改变子弹的颜色。有可能改变子弹的颜色吗?如果是,怎么做 颜色代码为0x53B0A2 我使用的是Xcode7.1.1,Swift2.0 您可以使用
UITextView
显示从API调用中获取的文本。从文本中,我需要识别特殊字符“,”(逗号),并将其替换为换行符转义序列和绿色的项目符号
我使用.stringByReplacingOccurrencesOfString(“,”和字符串:“\n•”
完成了这项工作。项目符号可从Edit->Emoji&Symbols
获取。它工作得很好
但我不知道如何改变子弹的颜色。有可能改变子弹的颜色吗?如果是,怎么做
颜色代码为0x53B0A2
我使用的是
Xcode
7.1.1,Swift2.0 您可以使用此功能:
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
您可以使用此功能:
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
在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
在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
使用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
}()
使用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
}()
NSAttributedString
我还没有用过。有没有关于如何使用它的想法?NSAttributedString
我还没有用过。有没有关于如何使用它的想法?