2016-07-07 8 views

答えて

2
//setting dummy text to label 
[email protected]"This is Simple Text With Red background Color"; 

//creating attributed string 
NSMutableAttributedString *attribString = 
[[NSMutableAttributedString alloc] initWithString:self.lbLog.text]; 

//setting background color to attributed text 
[attribString addAttribute:NSBackgroundColorAttributeName 
      value:[UIColor redColor] 
      range:NSMakeRange(0, attribString.length)]; 

//setting attributed text to label 
self.lbLog.attributedText = attribString; 
+0

@ Maheshあなたの返信をありがとうが、それは背景色を変えるだけです。実際には、隙間のある単一ラベルの各行に色を設定したいです。 – user3306145

+0

@ user3306145必要に応じて行の高さを設定できる 'NSParagraphAttributeName'と' NS(Mutable)ParagraphStyle'を使う必要があります。 – Larme

+0

しかし、私は別のフレームで2つのラベルのようにする必要があります。私は今、それはスペースの間にスペースを持っているが、私はクリアな色のスペースが欲しいur提案を実装した場合。 – user3306145

3

文字列の背景色に起因した文字列を使用して、その後に動作しない場合は、それらの間のスペースで2枚の別々のラベルを作成し、それぞれに背景色を設定する必要があるかもしれません。

3

テキストが終了した背景色でスペースが必要な場合があります。私は自分のトリックでこの問題を解決しました。カンマまたはポイントを追加して文字列を区切ります。これを文字列に適用します。余分なラベルを作成する必要はありません。

NSArray *aArray = [@" Font Size .k" componentsSeparatedByString:@".k"]; 

NSMutableAttributedString *fulltext=[[NSMutableAttributedString alloc] initWithString:@""]; 

NSMutableAttributedString *title1=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:0]]; 
NSMutableAttributedString *title2=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:1]]; 

//[title1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0] range:NSMakeRange(0,title1.length)]; 
[title1 addAttribute:NSBackgroundColorAttributeName 
       value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0] 
       range:NSMakeRange(0, title1.length)]; 
[title1 addAttribute:NSFontAttributeName 
       value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f] 
       range:NSMakeRange(0,title1.length)]; 

[title2 addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,title2.length)]; 
[title2 addAttribute:NSBackgroundColorAttributeName 
       value:[UIColor clearColor] 
       range:NSMakeRange(0, title2.length)]; 
[title2 addAttribute:NSFontAttributeName 
       value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f] 
       range:NSMakeRange(0,title2.length)]; 

[fulltext appendAttributedString:title1]; 
[fulltext appendAttributedString:title2]; 

self.textLabel.attributedText = fulltext; 

私の出力は次のようである:

enter image description here

テキストがあなたのスペースで終了した後、あなたがスペースを得ることができるので、今、Kの背景(TITLE2)をクリアを作ります!

2
lblText.backgroundColor=UIColor.red 
4

このコードのこの

NSString *[email protected]"What Does your friends really"; 
NSString *[email protected]"Think of your spouce?"; 

NSString *str1=[NSString stringWithFormat:@" %@..\n",yourString1]; 
NSString *str2=[NSString stringWithFormat:@" %@,,",yourString2]; 
NSString *str3=[NSString stringWithFormat:@"%@%@",str1,str2];  

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]]; 
NSRange range = [str1 rangeOfString:@".."]; 
NSRange range1 = [str3 rangeOfString:@",,"]; 
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range]; 
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range1];  
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor orangeColor] range: NSMakeRange(0, str1.length)]; 
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(str1.length, str2.length)]; 

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
[paragraphStyle setLineSpacing:5]; 
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str2 length])]; 

lblTest.attributedText = attributedString; 

出力のように行うことができます:あなたは、ラベルの背景色を設定し、別のラベルを使用する必要が

enter image description here

+0

あなたの出力はNSString * str1 = @ "あなたの友達は本当に何ですか? //最後にいくつかのスペースがありますか?エンドスペースの背景色をカバーする必要があります。しかし、あなたの解決策はありません。 –

+1

@JamshedAlam - 更新された回答を確認してください。 –

+0

これが解決策になるかもしれません。他の人を助けてくれてありがとう –

0

かの背景色すべてのラベルテキストのうち、ラベル内のすべてのテキストと同じです。そうでない場合は、異なる背景色のNSMutableAttributedStringを使用する必要があります。

label.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0]; 
+0

背景色が変わります。唯一のテキストカラーを変更する必要があります。 – user3306145

+0

label.textColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0]; – Miti

0

使う2つのカスタムラベル(またはあなたの背景として、代わりにラベルを使用するUIViewのは)、一つは明らかなように色を背景色設定し、あなたの背景のためのものであり、ウルテキストはあなたの欲求の色と背景色を設定するために別のラベルです。

0

私たちはUItextviewの助けを借りてこれを行うことができます。私はそれをしました、私はすぐにコードを掲載します。

関連する問題