gpt4 book ai didi

ios - 以特定方式将 String 转换为 NSAttributedString

转载 作者:行者123 更新时间:2023-12-01 17:37:11 24 4
gpt4 key购买 nike

所以我有一个看起来像这样的字符串:Swift , VisualBasic , Ruby
我想把这个字符串转换成这样的东西:
enter image description here

基本上我想在一个单词后面创建一个背景,是的,我可以使用 NSTokenField 库来获取这种行为,但是我的文本不是由用户手动输入的,它的预结构化(来自数组),我不想要 NSTokeField 的整个行为我只是想要这样的外观和选择(通过选择我的意思是单击退格键清除一个单词,整个单词不是一个字母)

好吧,我知道如何更改类似这样的文本颜色

   func getColoredText(text: String) -> NSMutableAttributedString {
let string:NSMutableAttributedString = NSMutableAttributedString(string: text)
let words:[String] = text.componentsSeparatedByString(" ")
var w = ""

for word in words {
if (word.hasPrefix("{|") && word.hasSuffix("|}")) {
let range:NSRange = (string.string as NSString).rangeOfString(word)
string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
w = word.stringByReplacingOccurrencesOfString("{|", withString: "")
w = w.stringByReplacingOccurrencesOfString("|}", withString: "")
string.replaceCharactersInRange(range, withString: w)
}
}
return string
}

但是我不知道如何实现我想要的,如果有人可以为我提供一些指导,那么它对我很有帮助

P.s 如果我的问题不够清楚,请告诉我,我会添加更多细节

最佳答案

使用几个 UILabel 会容易得多s 如果你想得到圆角。

如果这是可以接受的,您可以首先生成一个属性字符串数组,例如:

func getAttributedStrings(text: String) -> [NSAttributedString]
{
let words:[String] = text.componentsSeparatedByString(" , ")

let attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSBackgroundColorAttributeName: UIColor.blueColor()]

let attribWords = words.map({
return NSAttributedString(string: " \($0) ", attributes: attributes)
})
return attribWords
}

对于每个属性字符串,我们需要创建 UILabel .为此,我们可以创建一个传入 NSAttributedString 的函数。并返回 UILabel :
func createLabel(string:NSAttributedString) ->UILabel
{
let label = UILabel()
label.backgroundColor = UIColor.blueColor()
label.attributedText = string
label.sizeToFit()
label.layer.masksToBounds = true
label.layer.cornerRadius = label.frame.height * 0.5
return label
}

现在我们将输入字符串转换为标签:
let attribWords = getAttributedStrings("Java , Swift , JavaScript , Objective-C , Ruby , Pearl , Lisp , Haskell , C++ , C")
let labels = attribWords.map { string in
return createLabel(string)
}

现在我们只需要在 View 中显示它们:
let buffer:CGFloat = 3.0
var xOffset:CGFloat = buffer
var yOffset:CGFloat = buffer

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 400.0))
view.backgroundColor = UIColor.lightGrayColor()


for label in labels
{
label.frame.origin.x = xOffset
label.frame.origin.y = yOffset

if label.frame.maxX > view.frame.maxX
{
xOffset = buffer
yOffset += label.frame.height + buffer

label.frame.origin.x = xOffset
label.frame.origin.y = yOffset
}

view.addSubview(label)
xOffset += label.frame.width + buffer
}

在这一点上,我们还可以通过以下方式将 View 大小调整为标签的高度:
if let labelHeight = labels.last?.frame.height
{
view.frame.height = yOffset + labelHeight + buffer
}

将此代码扔到一个快速的操场上会导致:
enter image description here

如果你不能使用标签,如果你想要一个可编辑的 UITextView例如,我会放弃圆角,只说:
let attribWords = getAttributedStrings("Java , Swift , JavaScript , Objective-C , Ruby , Pearl , Lisp , Haskell , C++ , C")
let attribString = NSMutableAttributedString()
attribWords.forEach{
attribString.appendAttributedString(NSAttributedString(string: " "))
attribString.appendAttributedString($0)
}
textView.attributedText = attribString

关于ios - 以特定方式将 String 转换为 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37768686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com