我目前正在创建一个应用程序,显示用户在其视频中有多少喜欢/不喜欢的内容。我想我喜欢/不喜欢的文字有0.5个字母。这就是我的代码看起来像编辑标签中的特殊文本
我的代码看起来像这样,所以我的问题是如何使“喜欢/不喜欢”文本有0.5 alpha?
self.ratingLabel.text = @"%i likes %i dislikes", likes, dislikes;
我目前正在创建一个应用程序,显示用户在其视频中有多少喜欢/不喜欢的内容。我想我喜欢/不喜欢的文字有0.5个字母。这就是我的代码看起来像编辑标签中的特殊文本
我的代码看起来像这样,所以我的问题是如何使“喜欢/不喜欢”文本有0.5 alpha?
self.ratingLabel.text = @"%i likes %i dislikes", likes, dislikes;
首先创建一个颜色以0.5作为alpha分量,形成现有的颜色:
UIColor* color= [someColor colorWithAlphaComponent: 0.5];
或者从头开始:
UIColor* color= [UIColor colorWithRed: red green: green blue: blue alpha: 0.5];
然后,你必须创建一个属性串并将其设置为归因文字您的标签。用于前景颜色属性是NSForegroundColorAttributeName:
NSString* text= [NSString stringWithFormat: @"%i likes %i dislikes", likes, dislikes];
self.ratingLabel.attributedText= [[NSAttributedString alloc]initWithString: text attributes: @{ NSForegroundColorAttributeName : color}];
我试过这个,我把代码中的“文本”替换为@“likes”,它只显示没有其他东西。 这是我目前的代码 self.ratingLabel.text = [NSString stringWithFormat:@“%@ likes%@ dislikes”,formatLikes,formatdisLikes]; – IamGretar
@IamGretar此代码仍然不正确,因为您未创建属性字符串。您需要使用* NSForegroundAttributeName *创建一个属性字符串,如我的代码所示。 –
'[NSString的stringWithFormat:@ “%d%喜欢不喜欢d”,喜好,厌恶];'也许?那件事不会编译。 –
您需要使用正确的格式创建一个'NSAttributedString'并设置标签的'属性文字'属性。 – rmaddy
你能告诉我一个NSAttributedString的例子吗? – IamGretar