我正在使用属性字符串向我的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)
答案 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))