0

나는 메모 작성 응용 프로그램을 만들고 있습니다. 저장 기여 텍스트 On DetailVC 및 UITableViewCell 레이블에 미리보기를 표시합니다. 라벨에 swift - NSAttributedString에서 비어있는 (빈) 줄을 제거하려면 어떻게해야합니까?

내가이

The Bold Title 

my Text 

같은 텍스트를 가지고 내가 발견이 같은

The Bold Title 
my Text 

을 변경하려면 문자열에서 솔루션

let regex = try! NSRegularExpression(pattern: "\n+", options: []) 
let newString = regex.stringByReplacingMatches(in: MyString, options: [], range: NSRange(location: 0, length: MyString.characters.count), withTemplate: "\n") 

하지만, 어떻게해야합니까? NSAttirbutedStr?

pls help!

답변

1

NSMutableAttributedString은 내용의 변이를 허용하는 속성 문자열이며 그 다음에 mutableString 속성을 사용할 수 있습니다.

이미 NSAttributedString이있는 경우 mutableCopy()을 사용하거나 possible initializers of NSMutableAttributedString을 볼 수 있습니다.

let str = NSMutableAttributedString(string: "Hardcore\n\nHenry") 
str.mutableString.replaceOccurrences(of: "\n\n", with: "\n", options: [], range: NSMakeRange(0, str.length)) 
print(str) 
+0

정말 고마워요 !! 그러나 그것은 어떤 속성도 가지고 있지 않습니다! 이 문제를 어떻게 해결할 수 있습니까? – Daniel

+0

적합하다고 생각되는 속성을 추가 할 수 있습니다. 예를 들어'str.addAttribute (NSForegroundColorAttributeName, value : UIColor.red, range : NSMakeRange (0, 8))''(Swift2.x는'UIColor.redColor '하드 코어'레드. 'print()'에 표시되지 않습니다. UILabel이 어떻게 생겼는지 보려면 'UILabel'을 사용해야합니다. – Majster

+0

고마워요.하지만 내 문제는 속성이있는 텍스트를 대체하는 것입니다 ** ** 유지 자체 속성. (나는 UILabel을 사용했다 ...) – Daniel

관련 문제