StringExt
public extension String {
//Date format change
//ex -> "20171201".date(inputFormat: "yyyyMMdd", outputFormat: "yyyy.MM.dd")
func date(inputFormat: String, outputFormat:String) -> String? {
if self.isEmpty { return ""}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = inputFormat
if let date = dateFormatter.date(from: self) {
dateFormatter.dateFormat = outputFormat
return dateFormatter.string(from: date)
}
return self
}
//Highlight Ward
//ex -> "123".highlightSubwordWithColor("1", color: .red)
func highlightSubwordWithColor(_ searchWord:String, color:UIColor) -> NSAttributedString {
let attributedText = NSMutableAttributedString(string: self)
let nsstring = self as NSString
var range = nsstring.range(of: searchWord, options: NSString.CompareOptions.caseInsensitive, range: NSMakeRange(0, nsstring.length))
range = nsstring.range(of: searchWord, options: NSString.CompareOptions.caseInsensitive, range: NSMakeRange(0, nsstring.length))
while range.location != NSNotFound {
attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
range = nsstring.range(of: searchWord, options: NSString.CompareOptions.caseInsensitive, range: NSMakeRange(range.location+1, nsstring.length-range.location-1))
}
return attributedText
}
}
'iOS' 카테고리의 다른 글
[swift] Util(UIImageView) (0) | 2017.12.01 |
---|---|
[swift] Util(UIImage) (0) | 2017.12.01 |
[swift] iOS 주소 찾기 (0) | 2017.11.15 |
앱스토어 버전 가져오기 (0) | 2015.09.11 |
iOS AudioSession 공부중.... (0) | 2015.05.28 |