Swiftgram/submodules/TelegramUI/TelegramUI/AddFormatToStringWithRanges.swift
Peter b317aab568 Add 'submodules/TelegramUI/' from commit 'fa3ac0b61a27c8dd3296518a15891a6f9750cbf2'
git-subtree-dir: submodules/TelegramUI
git-subtree-mainline: 5c1613d1048026b9e00a6ce753775cef87eb53fa
git-subtree-split: fa3ac0b61a27c8dd3296518a15891a6f9750cbf2
2019-06-11 19:00:46 +01:00

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
}