iOS Swift:是否可以更改字符串中某个单词的字体样式?

11
我正在从数据库中提取字符串内容。使用一个方法,我提取出这个字符串中最长的单词。
现在,我想将整个字符串打印到文本标签中,并且希望在字符串中以不同的颜色和文本样式突出显示最长的单词。
我该怎么做? 我需要将字符串分段 - 设置格式 - 然后再将它们组合起来,然后将其提供给标签吗?
还是有其他更好的方法吗?

可能是NSAttributedString如何使用两种不同字体大小的重复问题。 - SwiftArchitect
3个回答

33
如果您已经知道最长的单词,您需要获取字符串中该单词的范围。我喜欢使用NSString方法rangeOfString:来实现这一点。
然后,您可以使用默认属性从字符串创建一个NSMutableAttributedString。最后,将高亮属性应用于先前计算出的范围。
let longString = "Lorem ipsum dolor. VeryLongWord ipsum foobar"
let longestWord = "VeryLongWord"

let longestWordRange = (longString as NSString).rangeOfString(longestWord)

let attributedString = NSMutableAttributedString(string: longString, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(20)])

attributedString.setAttributes([NSFontAttributeName : UIFont.boldSystemFontOfSize(20), NSForegroundColorAttributeName : UIColor.redColor()], range: longestWordRange)


label.attributedText = attributedString

Swift 5.0更新

let longestWordRange = (longString as NSString).range(of: longestWord)

let attributedString = NSMutableAttributedString(string: longString, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20)])

attributedString.setAttributes([NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 20), NSAttributedStringKey.foregroundColor : UIColor.red], range: longestWordRange)

这在我的游乐场上看起来像这样:

在此输入图片描述


太好了!谢谢!这正是我在寻找的! - user1555112
如果字符串中有重复字符,那么这段代码将无法正常工作。假设你有以下字符串:"VeryLongWord Lorem ipsum dolor. VeryLongWord ipsum foobar",并且你只需要更改在 "ipsum" 之前的 "VeryLongWord" 的字体。这段代码无法实现此功能。 - swift2geek

3

您需要了解属性字符串(Attributed Strings)和范围(NSRange)。这两者可以一起使用,为字符串中的范围创建不同的样式。以下是代码片段:

myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])

//Add more attributes here:
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Chalkduster", size: 24.0), range: NSRange(location: 7,length: 5))
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "AmericanTypewriter-Bold", size: 18.0)!, range: NSRange(location:2,length:4))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))

myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Georgia", size: 36.0)!, range: NSRange(location: 0, length: 1))
myMutableString.addAttribute(NSStrokeColorAttributeName, value: UIColor.blueColor(), range:  NSRange(location: 0, length: 1))
myMutableString.addAttribute(NSStrokeWidthAttributeName, value: 2, range: NSRange(location: 0, length: 1))

myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location: 0, length: myString.length))
myLabel.backgroundColor = UIColor.grayColor()

//Apply to the label
myLabel.attributedText = myMutableString

我相信你可以创建一个属性的 NSDictionary 并一次性添加所有属性。这样比逐个添加更加简洁 :) - Lord Zsolt
完全同意,我认为这一步骤对于初学NSAttributedString的人来说更可取。逐行阅读更容易 :)。不过我可能会考虑添加字典版本。谢谢。 - Chackle
@Chackle 听起来不错!我会查看一下的!我只需要弄清如何设置位置和长度...但这是一个好的开始! - user1555112

1

可变字符串。

您可以创建一个NSMutableAttributedString对象,并使用addAttributes:range方法应用所需的效果。

然后将其分配给UILabelattributedText属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接