0
그래서 스위프트 2에서 사용한 방법은 스위프트 3에서 문자열 인덱스와 범위와 관련하여 더 이상 작동하지 않습니다. 이전에 나는 기본적으로스위프트 3 : 문자열 부분에 속성을 적용하는 방법
func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) {
if let index = self.text?.characters.indexOf(Character("|")) {
self.text = self.text!.stringByReplacingOccurrencesOfString("|", withString: "")
let labelLength:Int = Int(String(index))! // Now returns nil
var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor]
var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor]
if boldKeyText {
keyAttr[NSFontAttributeName] = UIFont.systemFontOfSize(self.font.pointSize)
valAttr[NSFontAttributeName] = UIFont.systemFontOfSize(self.font.pointSize, weight: UIFontWeightHeavy)
}
let attributeString = NSMutableAttributedString(string: self.text!)
attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!))
attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength))
self.attributedText = attributeString
}
}
내가 같은 문자열을 취할 수 있습니다했다 "이름 : | 게리 오크" 및 이전과 을 한 후 모든 부품이 | 문자가 다른 색이거나 일부를 굵은 글씨로 나타내지 만 위에 설명한 줄은 더 이상 값을 반환하지 않으므로 나중에 다른 모든 부분이 깨집니다. 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 당신이 문자열 위치의 정수 표현하지만, 문자열 인덱스 값을하지있어 사용 let position = text.distance(from: text.startIndex, to: index)
것을
func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) {
if let index = self.text?.characters.index(of: Character("|")) {
self.text = self.text!.replacingOccurrences(of: "|", with: "")
let position = text.distance(from: text.startIndex, to: index)
let labelLength:Int = Int(String(describing: position))!
var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor]
var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor]
if boldKeyText {
keyAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize)
valAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize, weight: UIFontWeightHeavy)
}
let attributeString = NSMutableAttributedString(string: self.text!)
attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!))
attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength))
self.attributedText = attributeString
}
}
주요 아이디어 :