为什么我不能在Swift中用NSMutableString粗体化一部分字符串?

时间:2016-08-02 22:27:50

标签: ios swift

问题是我可以为字符串的一半着色,但我不能将字符串的一半加粗。

这是代码

 var string = "blah blah blah blah blah"
  var range = NSRange(location:0, length: 4)
//Apply to the label
myMutableString.addAttribute(NSFontAttributeName,
                             value: UIFont.boldSystemFontOfSize([X size]), range: range)

myMutableString.addAttribute(NSForegroundColorAttributeName,
                             value: UIColor.green(),
                             range: range)
UILabel.text.attributedText = myMutableString

任何想法为什么粗体加粗整个字符串而不是第一个“blah”?只有第一个“blah”显示为绿色。

2 个答案:

答案 0 :(得分:3)

我只是发布了一个答案,因为以交互方式使用游乐场并看到UILabel实时变化很有趣。

import UIKit

let label = UILabel(frame: CGRectMake(0, 0, 300, 100))
let text = "black green small"
var mutableString = NSMutableAttributedString(string: text)

let textCount = text.utf16.count // Thanks OOPer
mutableString.addAttribute(NSFontAttributeName,
    value: UIFont.boldSystemFontOfSize(20),
    range: NSRange(location:0, length: textCount*2/3)
)

mutableString.addAttribute(NSForegroundColorAttributeName,
    value: UIColor.greenColor(),
    range: NSRange(textCount/3..<textCount)
)

label.attributedText = mutableString
label

Image of interactive .playground file

为了取悦OOPer,我对字符索引有了更多的了解

//: Playground - noun: a place where people can play

import UIKit

extension String { // tools
    func characterIndex(ofFraction fraction: Float) -> String.CharacterView.Index {
        let amountOfCharacters = Int(Float(self.characters.count) * fraction)
        return self.characters.startIndex.advancedBy(amountOfCharacters, limit: self.characters.endIndex)
    }

    func utf16Index(characterIndex index: String.CharacterView.Index) -> String.UTF16View.Index {
        return String.UTF16View.Index(index, within: self.utf16)
    }

    func utf16Offset(toIndex index: String.UTF16View.Index) -> String.UTF16View.Index.Distance {
        return self.utf16.startIndex.distanceTo(index)
    }
}

extension String { // convenience
    func utf16Offset(ofFraction fraction: Float) -> String.UTF16View.Index.Distance {
        let correctIndexOfFraction = self.characterIndex(ofFraction: fraction)
        let translatedToUTF16 = self.utf16Index(characterIndex: correctIndexOfFraction)
        let absolutePosition = self.utf16Offset(toIndex: translatedToUTF16)
        return absolutePosition
    }
}

func paintedString(text: String) -> NSAttributedString {
    let mutableString = NSMutableAttributedString(string: text)

    let firstTwoThirds = 0..<text.utf16Offset(ofFraction: 2/3)
    let lastTwoThirds = text.utf16Offset(ofFraction: 1/3)..<text.utf16.count

    mutableString.addAttribute(NSFontAttributeName,
                               value: UIFont.boldSystemFontOfSize(30),
                               range: NSRange(firstTwoThirds)
    )

    mutableString.addAttribute(NSForegroundColorAttributeName,
                               value: UIColor.greenColor(),
                               range: NSRange(lastTwoThirds)
    )

    return mutableString
}

func paintedLabel(text text: String) -> UILabel {
    let label = UILabel(frame: CGRectMake(0, 0, 300, 50))
    label.attributedText = paintedString(text)
    return label
}

paintedLabel(text: "black green small")
paintedLabel(text: "")

New correct version executed in Playground

答案 1 :(得分:2)

您的代码无效,因为即使在将属性设置为myMutableString之前,也应将字符串分配给myMutableString

var string = "blah blah blah blah blah"

myMutableString.addAttribute(NSFontAttributeName,
                             value: UIFont.boldSystem .. 

基于您的要求的工作代码:

var string = "blah blah blah blah blah"

let myMutableString = NSMutableAttributedString(string: string)
 var range = NSRange(location:0, length: 4)
 //Apply to the label
 myMutableString.addAttribute(NSFontAttributeName,
                              value: UIFont.boldSystemFontOfSize(10), range: range)

 myMutableString.addAttribute(NSForegroundColorAttributeName,
                              value: UIColor.blueColor(),
                              range: range)