2016-06-11 23 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

相關問題