SOのこの質問にはいくつかのバリエーションがありますが、Swift 4の最新の回答はないようです... UIButtonのタイトルを文字列の一部を太字で示し、一部ではない属性付き文字列。私はSwift 4の新しい範囲に基づいてこのコードを試しています。 しかし、コードは部分文字列に影響しないようです。誰でもこれが働いていますか? 'UIButtonのタイトルを変更するattributedStrings enumerateSubstrings文字列の範囲
let text = NSLocalizedString(" BOLD TEXT not \r\n bold text", comment: "")
let attributedString = NSMutableAttributedString(string: text)
text.enumerateSubstrings(in: text.startIndex..<text.endIndex, options: .byWords) {
(substring, substringRange, _, _) in
if substring == "BOLD TEXT" {
attributedString.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 30),
range: NSRange(substringRange, in: text))
}
}
cell.multiFunctionButton.setAttributedTitle(attributedString, for: .normal)`
@larmeはBOLDの下に2つの単語があります。
let str = NSString(string: text)
let theRange = str.range(of: "BOLD TEXT")
let attribute = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20),
NSAttributedStringKey.foregroundColor: UIColor.red]
attributedString.addAttributes(attribute, range: theRange)
印刷 'substring'、あなたはなぜそれがかもしれ表示されますうまくいかない。あなたは単語を列挙していますが、 "BOLD TEXT"は2単語なので、 'substring'は" BOLD TEXT "と決して等しくないかもしれません。さて、あなたはlocalizedStringを使用しているので、私は "may"を使用しましたが、その背後にある値は何か分かりません。 – Larme
あなたが期待する正確な結果 - ここでスナップショット(画像)を共有してより良いヘルプを得る – Krunal