草庐IT

swift - UITextView linkTextAttributes 字体属性不适用于 NSAttributedString

coder 2023-07-15 原文

我有一个从 HTML 生成的 NSAttributedString,其中包含一些链接。属性字符串显示在 UITextView 中。我希望为链接应用不同的字体样式并为此设置 linkTextAttributes。我添加了 NSForegroundColorAttributeNameNSFontAttributeNameNSUnderlineStyleAttributeName。由于某种原因,应用了前景色,但没有应用其余属性。

myTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.redColor(), NSFontAttributeName : textLinkFont, NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleNone.rawValue]

有没有其他人遇到过这个问题?如何更改链接的字体样式而不必将内联 CSS 应用于原始 HTML?谢谢。

最佳答案

不确定为什么 linkTextAttributes 不适用于字体名称。但是我们可以通过更新 NSAttributedString 的链接属性来实现。检查下面的代码。

        do {
        let htmlStringCode = "For more info <a href=\"http://www.samplelink.com/subpage.php?id=8\">Click here</a>"

        let string = try NSAttributedString(data: htmlStringCode.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)

        let newString = NSMutableAttributedString(attributedString: string)
        string.enumerateAttributesInRange(NSRange.init(location: 0, length: string.length), options: .Reverse) { (attributes : [String : AnyObject], range:NSRange, _) -> Void in
            if let _ = attributes[NSLinkAttributeName] {
                newString.removeAttribute(NSFontAttributeName, range: range)
                newString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(30), range: range)
            }
        }
        textField.attributedText = newString
        textField.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.redColor(), NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleNone.rawValue]

    }catch {
    }

这是 objective-C 代码:

NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType};
NSData *data = [html dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
NSMutableAttributedString *attributedStringWithBoldLinks = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];

[attributedString enumerateAttributesInRange:NSMakeRange(0, attributedString.string.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {

    if ([attrs objectForKey:NSLinkAttributeName]) {
        [attributedStringWithBoldLinks removeAttribute:NSFontAttributeName range:range];
        [attributedStringWithBoldLinks addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"YourFont-Bold" size:16.0] range:range];
    }
}];

self.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor redColor]};

self.attributedText = attributedStringWithBoldLinks;

关于swift - UITextView linkTextAttributes 字体属性不适用于 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35053694/

有关swift - UITextView linkTextAttributes 字体属性不适用于 NSAttributedString的更多相关文章

随机推荐