UITextView 中字符串的粗体部分 swift
bold part of string in UITextView swift
正文如下:
@IBOutlet weak var legalText: UITextView!
let textToAppend = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
legalText.text = legalText.text.stringByAppendingString(textToAppend)
我想加粗 "TERMS OF SERVICE" 和 "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
我尝试在 uitextview 中以编程方式使用 uilabel,但没有成功:
var termsofservice : UILabel = UILabel()
termsofservice.numberOfLines = 0
termsofservice.text = "TERMS OF SERVICE"
termsofservice.font = UIFont.boldSystemFontOfSize(20)
termsofservice.textAlignment = NSTextAlignment.Left;
var pleasenote : UILabel = UILabel()
pleasenote.numberOfLines = 0
pleasenote.text = "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
pleasenote.font = UIFont.boldSystemFontOfSize(20)
pleasenote.textAlignment = NSTextAlignment.Left;
let textToAppend = "\(termsofservice)\nLast Updated: May 7, 2015\n\n\(pleasenote)"
也试过这些但没有用,它只显示 "TERMS OF SERVICE" 和 "last update..",没有显示 "PLEASE NOTE..."
var termsofservice = "TERMS OF SERVICE"
var normalText = "\n\nLast Updated: May 7, 2015"
var pleasenote = "\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
var attributedString = NSMutableAttributedString(string:normalText)
var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
var boldString = NSMutableAttributedString(string:pleasenote, attributes:attrs)
var boldString0 = NSMutableAttributedString(string:termsofservice, attributes:attrs)
boldString0.appendAttributedString(attributedString)
attributedString.appendAttributedString(boldString)
legalText.attributedText = boldString0
如何将字符串的那部分加粗?
注意:文字仍然很长,字符串的更多部分要加粗。
更新 Swift 3
func attributedText() -> NSAttributedString {
let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString
let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]
// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))
// 4
return attributedString
}
并将 attributetext 文本设置为您的文本视图:
legalText.attributedText = attributedText()
更好的是,您可以继续在您的军械库中使用这个 1 函数,在像 Constants.swift 这样的文件中,然后您可以在许多情况下通过调用 [=18= 来增强任何字符串中的单词]一行代码:
要进入没有 class 的文件,例如 constants.swift 在我的例子中:
import Foundation
import UIKit
func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
let nonBoldFontAttribute = [NSAttributedString.Key.font:font!]
let boldFontAttribute = [NSAttributedString.Key.font:boldFont!]
let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
boldString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartOfString as String))
return boldString
}
然后你可以为任何 UILabel 调用这一行代码:
let normalFont = UIFont(name: "INSERT FONT NAME", size: 14)
let boldFont = UIFont(name: "INSERT BOLD FONT NAME", size: 14)
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldFont!)
Swift 3更新(来自@Hamza Ansari的回答)
func attributedText()-> NSAttributedString
{
let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString
let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]
// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))
// 4
return attributedString
}
Swift 4.2 更新
func addBoldText(fullString: String, boldPartOfString: String, baseFont: UIFont, boldFont: UIFont) -> NSAttributedString {
//1
let baseFontAttribute = [NSAttributedString.Key.font : baseFont]
let boldFontAttribute = [NSAttributedString.Key.font : boldFont]
//2
let attributedString = NSMutableAttributedString(string: fullString, attributes: baseFontAttribute)
//3. Note if 'boldPartOfString' is not a substring of 'fullString', enitre 'fullString' will be formatted with 'boldFont'
attributedString.addAttributes(boldFontAttribute, range: NSRange(fullString.range(of: boldPartOfString) ?? fullString.startIndex..<fullString.endIndex, in: fullString))
//4
return attributedString
}
正文如下:
@IBOutlet weak var legalText: UITextView!
let textToAppend = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
legalText.text = legalText.text.stringByAppendingString(textToAppend)
我想加粗 "TERMS OF SERVICE" 和 "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
我尝试在 uitextview 中以编程方式使用 uilabel,但没有成功:
var termsofservice : UILabel = UILabel()
termsofservice.numberOfLines = 0
termsofservice.text = "TERMS OF SERVICE"
termsofservice.font = UIFont.boldSystemFontOfSize(20)
termsofservice.textAlignment = NSTextAlignment.Left;
var pleasenote : UILabel = UILabel()
pleasenote.numberOfLines = 0
pleasenote.text = "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
pleasenote.font = UIFont.boldSystemFontOfSize(20)
pleasenote.textAlignment = NSTextAlignment.Left;
let textToAppend = "\(termsofservice)\nLast Updated: May 7, 2015\n\n\(pleasenote)"
也试过这些但没有用,它只显示 "TERMS OF SERVICE" 和 "last update..",没有显示 "PLEASE NOTE..."
var termsofservice = "TERMS OF SERVICE"
var normalText = "\n\nLast Updated: May 7, 2015"
var pleasenote = "\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
var attributedString = NSMutableAttributedString(string:normalText)
var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
var boldString = NSMutableAttributedString(string:pleasenote, attributes:attrs)
var boldString0 = NSMutableAttributedString(string:termsofservice, attributes:attrs)
boldString0.appendAttributedString(attributedString)
attributedString.appendAttributedString(boldString)
legalText.attributedText = boldString0
如何将字符串的那部分加粗?
注意:文字仍然很长,字符串的更多部分要加粗。
更新 Swift 3
func attributedText() -> NSAttributedString {
let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString
let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]
// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))
// 4
return attributedString
}
并将 attributetext 文本设置为您的文本视图:
legalText.attributedText = attributedText()
更好的是,您可以继续在您的军械库中使用这个 1 函数,在像 Constants.swift 这样的文件中,然后您可以在许多情况下通过调用 [=18= 来增强任何字符串中的单词]一行代码:
要进入没有 class 的文件,例如 constants.swift 在我的例子中:
import Foundation
import UIKit
func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
let nonBoldFontAttribute = [NSAttributedString.Key.font:font!]
let boldFontAttribute = [NSAttributedString.Key.font:boldFont!]
let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
boldString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartOfString as String))
return boldString
}
然后你可以为任何 UILabel 调用这一行代码:
let normalFont = UIFont(name: "INSERT FONT NAME", size: 14)
let boldFont = UIFont(name: "INSERT BOLD FONT NAME", size: 14)
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldFont!)
Swift 3更新(来自@Hamza Ansari的回答)
func attributedText()-> NSAttributedString
{
let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString
let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]
// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))
// 4
return attributedString
}
Swift 4.2 更新
func addBoldText(fullString: String, boldPartOfString: String, baseFont: UIFont, boldFont: UIFont) -> NSAttributedString {
//1
let baseFontAttribute = [NSAttributedString.Key.font : baseFont]
let boldFontAttribute = [NSAttributedString.Key.font : boldFont]
//2
let attributedString = NSMutableAttributedString(string: fullString, attributes: baseFontAttribute)
//3. Note if 'boldPartOfString' is not a substring of 'fullString', enitre 'fullString' will be formatted with 'boldFont'
attributedString.addAttributes(boldFontAttribute, range: NSRange(fullString.range(of: boldPartOfString) ?? fullString.startIndex..<fullString.endIndex, in: fullString))
//4
return attributedString
}