mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-02 00:17:02 +00:00

git-subtree-dir: submodules/TelegramUI git-subtree-mainline: 5c1613d1048026b9e00a6ce753775cef87eb53fa git-subtree-split: fa3ac0b61a27c8dd3296518a15891a6f9750cbf2
30 lines
1.5 KiB
Swift
30 lines
1.5 KiB
Swift
import Foundation
|
|
import UIKit
|
|
|
|
func addAttributesToStringWithRanges(_ stringWithRanges: (String, [(Int, NSRange)]), body: MarkdownAttributeSet, argumentAttributes: [Int: MarkdownAttributeSet], textAlignment: NSTextAlignment = .natural) -> NSAttributedString {
|
|
let result = NSMutableAttributedString()
|
|
|
|
var bodyAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: body.font, NSAttributedStringKey.foregroundColor: body.textColor, NSAttributedStringKey.paragraphStyle: paragraphStyleWithAlignment(textAlignment)]
|
|
if !body.additionalAttributes.isEmpty {
|
|
for (key, value) in body.additionalAttributes {
|
|
bodyAttributes[NSAttributedStringKey(rawValue: key)] = value
|
|
}
|
|
}
|
|
|
|
result.append(NSAttributedString(string: stringWithRanges.0, attributes: bodyAttributes))
|
|
|
|
for (index, range) in stringWithRanges.1 {
|
|
if let attributes = argumentAttributes[index] {
|
|
var argumentAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: attributes.font, NSAttributedStringKey.foregroundColor: attributes.textColor, NSAttributedStringKey.paragraphStyle: paragraphStyleWithAlignment(textAlignment)]
|
|
if !attributes.additionalAttributes.isEmpty {
|
|
for (key, value) in attributes.additionalAttributes {
|
|
argumentAttributes[NSAttributedStringKey(rawValue: key)] = value
|
|
}
|
|
}
|
|
result.addAttributes(argumentAttributes, range: range)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|