Код: Выделить всё
extension NSMutableAttributedString {
func bold(_ text: String, font: UIFont = .systemFont(ofSize: 17, weight: .bold)) -> Self {
let range = self.range(of: text)!
self.addAttribute(.font, value: font, range: range) //error 'Value of type self has no member range'
return self
}
}
public extension String {
func attributedString(with boldText: String, boldTextStyle: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 17)]) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: self)
let boldTextRange = range(of: boldText)!
attributedString.addAttributes(boldTextStyle, range: boldTextRange) //Cannot convert value of type 'Range' to expected argument type 'NSRange' (aka '_NSRange')
return attributedString
}
}
Код: Выделить всё
NSString *normalText = @"Hello ";
NSString *boldText = @"world";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:normalText];
NSDictionary *attrs = @{ NSAttributedString.Key.font: [UIFont boldSystemFontOfSize:15] };//throws 5 errors
NSMutableAttributedString *boldString = [[NSMutableAttributedString alloc] initWithString:boldText attributes:attrs];//throws 5 errors
[attributedString appendAttributedString:boldString];
Может ли кто-нибудь подсказать, как заставить вышеизложенное работать в современный Swift, за исключением SwiftUI, или как создать расширение строки, чтобы выделить часть строки. Спасибо за любые предложения.
Подробнее здесь: https://stackoverflow.com/questions/790 ... on-in-2024