Created
January 10, 2015 07:43
-
-
Save codetalks-new/457436724a294903fd21 to your computer and use it in GitHub Desktop.
a Simple AttributedString Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TextCommentView:UILabel{ | |
var name :String = ""{ | |
didSet{ | |
updateText() | |
} | |
} | |
var comment:String = ""{ | |
didSet{ | |
updateText() | |
} | |
} | |
func updateText(){ | |
numberOfLines = 2 | |
let nameAttr:NSDictionary = [ | |
NSFontAttributeName:UIFont.systemFontOfSize(13), | |
NSForegroundColorAttributeName:blueColor | |
] | |
let nameAttrString = NSAttributedString(string: name, attributes: nameAttr) | |
let commentAttr:NSDictionary = [ | |
NSFontAttributeName:UIFont.systemFontOfSize(12), | |
NSForegroundColorAttributeName:UIColor.darkGrayColor() | |
] | |
let commentAttrString = NSAttributedString(string: " :"+comment, attributes: commentAttr) | |
let attrString = NSMutableAttributedString(attributedString: nameAttrString) | |
attrString.appendAttributedString(commentAttrString) | |
self.attributedText = attrString | |
layoutIfNeeded() | |
} | |
} | |
let textCommentView = TextCommentView(frame: CGRect(x: 0, y: 0, width: 240, height: 60)) | |
textCommentView.backgroundColor = UIColor.whiteColor() | |
textCommentView.comment = demoSummary() | |
textCommentView.name = demoName() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment