gpt4 book ai didi

ios - 用 attributedText 给 UILabel 上色

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:11 25 4
gpt4 key购买 nike

你好我正在做一个 Twitter 应用程序,我想给主题标签、用户名和 URL 上色这是我的手机上的课

class TweetTableViewCell: UITableViewCell
{
var tweet: Tweet? {
didSet {
updateUI()
}
}

@IBOutlet weak var tweetProfileImageView: UIImageView!
@IBOutlet weak var tweetScreenNameLabel: UILabel!
@IBOutlet weak var tweetTextLabel: UILabel!
@IBOutlet weak var tweetCreatedLabel: UILabel!

func updateUI() {

tweetTextLabel?.attributedText = nil
tweetScreenNameLabel?.text = nil
tweetProfileImageView?.image = nil
tweetCreatedLabel?.text = nil


if let tweet = self.tweet
{
tweetTextLabel?.text = tweet.text
if tweetTextLabel?.text != nil {
for _ in tweet.media {
tweetTextLabel.text! += " 📷"
}
for user in tweet.userMentions{

tweetTextLabel.attributedText = NSAttributedString(string:"\(user.keyword)")




}
}

tweetScreenNameLabel?.text = "\(tweet.user)"

if let profileImageURL = tweet.user.profileImageURL {
let qos = Int(QOS_CLASS_USER_INITIATED.value)
dispatch_async(dispatch_get_global_queue(qos, 0)) { () -> Void in
if let imageData = NSData(contentsOfURL: profileImageURL) {
dispatch_async(dispatch_get_main_queue()){
if profileImageURL == tweet.user.profileImageURL{
self.tweetProfileImageView?.image = UIImage(data: imageData)
}
}}
}
}


let formatter = NSDateFormatter()
if NSDate().timeIntervalSinceDate(tweet.created) > 24*60*60 {
formatter.dateStyle = NSDateFormatterStyle.ShortStyle
} else {
formatter.timeStyle = NSDateFormatterStyle.ShortStyle
}
tweetCreatedLabel?.text = formatter.stringFromDate(tweet.created)
}

}

问题就在这里

 for user in tweet.userMentions{
tweetTextLabel.attributedText = NSMutableAttributedString(string:"\(user.keyword)")

}

我不知道如何给它上色(attributedText)。我有带主题标签的字符串,用户名。我正在尝试使用用户名

更新

for user in tweet.userMentions{
//tweetTextLabel.attributedText = NSMutableAttributedString(string:"\(user.keyword)")
var attributedText = tweetTextLabel.attributedText
var userName:NSString = user.keyword
var atrString = NSMutableAttributedString(string:tweetTextLabel.text!)
atrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: fullString.rangeOfString(userName))
attributedText = atrString


}

并且是这样说的;它之前是有效的:使用未解析的标识符“fullString”

最佳答案

试试这个:

var attrs = [NSForegroundColorAttributeName : UIColor.redColor()]
var atrText = NSMutableAttributedString(string:"text", attributes:attrs)
tweetTextLabel.attributedText = atrText;

关于ios - 用 attributedText 给 UILabel 上色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com