2016-06-11 22 views
1

,所以我有一个字符串,它看起来像这样:Swift , VisualBasic , Ruby一个字符串转换为NSAttributedString以特定的方式

我想这个字符串转换为这样的事情: 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 
} 

的颜色,但我不知道该如何实现我想要什么,如果有人能提供我一些指导,那么它会是这样的帮助我

私人秘书,如果我的问题不够清楚,那么请让我知道我会添加一些更多的细节

回答

4

这将是非常容易,只需使用几个UILabel■如果你想获得圆角。

如果这是可以接受的,您可以先产生归因字符串像数组:

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

let attribWords = getAttributedStrings("Java , Swift , JavaScript , Objective-C , Ruby , Pearl , Lisp , Haskell , C++ , C") 
let labels = attribWords.map { string in 
     return createLabel(string) 
    } 

现在我们只需要:

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

我们还可以在这一点上说调整我们对标签的高度视图:

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 
+0

@beyouWulf感谢人其工作很好,我想知道我们可以计算所有标签(组合)的高度?任何想法我们怎么能? –

+1

我更新了我的答案。在完成标签布​​局后,很容易就可以得到它。您可以将'for'循环片段和'if let'片段合并为一个函数,如'layoutLabelsAndResizeView'或类似的东西。 – beyowulf

相关问题