在UILabel中更改属性字符串的文本

时间:2016-04-02 17:27:00

标签: ios swift uilabel nsattributedstring

我正在使用属性字符串向我的UILabel添加一些文字,并且我想在用户按下UILabel时更改此文字。

这是我的代码:

let likes: UILabel = UILabel(frame: CGRectMake(5, CGFloat(totalHeight + 5), 50, 21));
let likesString: NSAttributedString = NSAttributedString (string: String(selectedItem.noLikes))
var myString: NSMutableAttributedString = NSMutableAttributedString(string: " ")
myString.appendAttributedString(likesString)
likes.attributedText = myString
mainScrollView.addSubview(likes)

我想将likesString的值从(selectedItem.noLikes)更改为(selectedItem.noLikes + 1)

1 个答案:

答案 0 :(得分:0)

试试此代码

//I assume that you are getting like label instance on tap action on it

 let likesString: NSAttributedString = likes.attributedText
 let str:NSString = likesString.string

 var intValue:Int = str.integerValue
 intValue = intValue + 1 //increased value by 1

likes.attributedText = NSAttributedString (string: String(intValue))