如何在swift中更改NSAttributedString的字体大小和字体颜色?
@IBOutlet weak var detailsTextView: UITextView!
if let data = htmltext.data(using: String.Encoding.utf8, allowLossyConversion: true)
{
do
{
let newFont = UIFont(name: "Georgia", size: 22.0) ?? UIFont()
let attributedText = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
detailsTextView.attributedText = attributedText
}
catch{}
}
htmltext是需要设置为UILabel的html值
答案 0 :(得分:0)
你就是这样做的:
table(stack(setNames(strsplit(as.character(df$type), ","), df$year))[2:1])
# values
#ind a b c d e f g h i
# 2012 2 1 3 2 1 1 0 0 0
# 2013 4 1 1 1 0 0 0 0 0
# 2014 1 1 0 0 1 0 2 1 1
答案 1 :(得分:0)
这是设置NSAttributedString的属性的方法:
add_action( 'after_setup_theme', 'register_theme_menu' );
function register_theme_menu() {
register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}
答案 2 :(得分:0)
使用此
let newAttributedString = NSMutableAttributedString(attributedString: label.attributedText)
// Enumerate through all the font ranges
newAttributedString.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0,
newAttributedString.length), options: []) { value, range, stop in
guard let currentFont = value as? UIFont else {
return
}
// An NSFontDescriptor describes the attributes of a font: family name, face name, point size, etc.
// Here we describe the replacement font as coming from the "Hoefler Text" family
let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptorFamilyAttribute: "Hoefler Text"])
// Ask the OS for an actual font that most closely matches the description above
if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptorFamilyAttribute]).first {
let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
newAttributedString.addAttributes([NSFontAttributeName: newFont], range: range)
}
label.attributedText = newAttributedString
}
答案 3 :(得分:0)
在String
扩展名中添加以下属性:
extension String
{
var htmlToAttributedString: NSAttributedString?
{
guard let data = data(using: .utf8) else { return nil }
do
{
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .right
let content = try NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
content.addAttributes([NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.alJazeera(.regular(20)),
NSAttributedString.Key.foregroundColor: UIColor.appDark],
range: NSMakeRange(0, content.length))
return content
}
catch
{
return nil
}
}
}
然后轻松使用它:
textView.attributedText = myText.htmlToAttributedString
注意::
UIFont.alJazeera(.regular(20))
是在UIFont
扩展名中定义的静态方法,而UIColor.appDark
也是在UIColor
扩展名中定义的静态属性
答案 4 :(得分:-1)
你可以这样试试,
let finaltext = "<span style='font-family:Georgia; font-size: 20px'>\(Your html string)</span>"
detailsTextView.attributedText = finaltext.html2AttributedString
并添加扩展程序
extension String {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print(error)
return nil
}
}
}