I have a similar question that when layout, some attributes of NSAttributedString make no effect and some others make effects not expected, so how to deal with them?
In my app, I need to increase the line space in paragraphs. Therefore, I set the paragraphStyle like this
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.lineHeightMultiple = 1.6
paragraphStyle.paragraphSpacing = defaultFontSize * 2.0
paragraphStyle.hyphenationFactor = 1
for range in allMyRanges {
attributedString.removeAttribute(NSAttributedString.Key.paragraphStyle, range: range)
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
}
When layout, all paragraphs with this paragraphStyle had the wrong frames and were also half covered by the paragraphs following them, like this:
Is there a way to control the frame of layoutFragments?
Post
Replies
Boosts
Views
Activity
I figured out a clumsy way to convert NSTextRange to NSRange, of cause by researching the development documents for ages.
let offset = textContentStorage!.offset(from: textLayoutManager!.documentRange.location, to: textRange.location)
let length = textContentStorage!.offset(from: textRange.location, to: textRange.endLocation)
let nsRange = NSRange(location: offset, length: length)