【问题标题】:bold part of string in UITextView swiftUITextView swift中字符串的粗体部分
【发布时间】:2015-10-17 07:37:18
【问题描述】:

这是正文:

@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)

我想加粗“服务条款”和“请注意:以下 HIGO 服务条款自上述“最后更新”日期起对任何浏览 HIGO 网站的用户或任何用户生效谁在该日期或之后创建了 HIGO 帐户。”

我尝试在 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)"

也尝试使用这些但没有用,它只显示“服务条款”和“上次更新..”,没有显示“请注意...”

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

如何将那部分字符串加粗?

注意:文本仍然很长,并且有更多部分字符串要加粗。

【问题讨论】:

  • 你应该使用属性字符串
  • 怎么样?请给我看。
  • 当然,您的第二个代码不会显示出令人愉快的部分。您将其附加到错误的字符串。
  • attributedString.appendAttributedString(boldString)boldString0.appendAttributedString(boldString)

标签: swift uitextview


【解决方案1】:

为 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
}

并将属性文本设置为您的文本视图:

legalText.attributedText = attributedText()

【讨论】:

  • 这几乎是正确的,但是rangeOfString 返回Range,而addAttributes 需要NSRange,所以你需要稍微玩一下。无论如何,+1
  • 如果您将字符串用作 NSString,rangeOfString 将返回 NSRange,希望它能消除您的疑虑。
  • 没有错误,字符串只是没有呈现我提供的 NSFontAttributeName。
  • 对于 Swift 4: let attributesString = NSMutableAttributedString(string: string as String, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15.0)]) let boldFontAttribute = [NSAttributedStringKey.font : UIFont. boldSystemFont(ofSize: 15.0)]
【解决方案2】:

更好的是,你可以继续在你的军械库中使用这个 1 函数,在一个像 Constants.swift 这样的文件中,然后你可以在很多情况下通过调用 ONE LINE来加粗任何字符串中的单词> 代码:

进入一个没有类的文件,比如我的例子中的 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!)

【讨论】:

  • 我知道这是旧的,但有没有办法用它来加粗同一字符串的两个不同部分?
【解决方案3】:

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
}

【讨论】:

    【解决方案4】:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多