添付画像のようにラベルを設定する必要があるアプリケーションを開発しています。だれか考えがあれば教えてください。 UILabelテキストのみに背景色を設定するには
答えて
//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;
@ Maheshあなたの返信をありがとうが、それは背景色を変えるだけです。実際には、隙間のある単一ラベルの各行に色を設定したいです。 – user3306145
@ user3306145必要に応じて行の高さを設定できる 'NSParagraphAttributeName'と' NS(Mutable)ParagraphStyle'を使う必要があります。 – Larme
しかし、私は別のフレームで2つのラベルのようにする必要があります。私は今、それはスペースの間にスペースを持っているが、私はクリアな色のスペースが欲しいur提案を実装した場合。 – user3306145
文字列の背景色に起因した文字列を使用して、その後に動作しない場合は、それらの間のスペースで2枚の別々のラベルを作成し、それぞれに背景色を設定する必要があるかもしれません。
テキストが終了した背景色でスペースが必要な場合があります。私は自分のトリックでこの問題を解決しました。カンマまたはポイントを追加して文字列を区切ります。これを文字列に適用します。余分なラベルを作成する必要はありません。
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;
私の出力は次のようである:
テキストがあなたのスペースで終了した後、あなたがスペースを得ることができるので、今、Kの背景(TITLE2)をクリアを作ります!
lblText.backgroundColor=UIColor.red
このコードのこの
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;
出力のように行うことができます:あなたは、ラベルの背景色を設定し、別のラベルを使用する必要が
あなたの出力はNSString * str1 = @ "あなたの友達は本当に何ですか? //最後にいくつかのスペースがありますか?エンドスペースの背景色をカバーする必要があります。しかし、あなたの解決策はありません。 –
@JamshedAlam - 更新された回答を確認してください。 –
これが解決策になるかもしれません。他の人を助けてくれてありがとう –
かの背景色すべてのラベルテキストのうち、ラベル内のすべてのテキストと同じです。そうでない場合は、異なる背景色のNSMutableAttributedStringを使用する必要があります。
label.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0];
背景色が変わります。唯一のテキストカラーを変更する必要があります。 – user3306145
label.textColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0]; – Miti
使う2つのカスタムラベル(またはあなたの背景として、代わりにラベルを使用するUIViewのは)、一つは明らかなように色を背景色設定し、あなたの背景のためのものであり、ウルテキストはあなたの欲求の色と背景色を設定するために別のラベルです。
私たちはUItextviewの助けを借りてこれを行うことができます。私はそれをしました、私はすぐにコードを掲載します。
- 1. iOS UILabelのテキストの背景色を設定する(丸い四角形)
- 2. wxTreeListCtrlでテキストの背景色を設定するには
- 3. すぐにUIlabelの背景色の高さを設定する方法
- 4. 背景色を設定
- 5. swiftでUILabelの背景色を変更するには
- 6. セクションヘッダーをカスタマイズするuilabel背景とテキスト
- 7. 背景色のサイズを設定する
- 8. グラフの背景色を設定する
- 9. UIVIewの背景色を設定する
- 10. ルートウィンドウの背景色を設定する
- 11. JPanelの背景色を設定する
- 12. 列の背景色を設定する
- 13. ウェブマークアップ、背景色をベースに設定
- 14. 背景色に基づくテキストの色
- 15. JTextPaneの行にカスタム背景色を設定するには
- 16. iReportのページ全体に背景色を設定するには?
- 17. 透明度を設定したテキストの背景色を設定する
- 18. アクティブなタブのみの背景色を設定する方法
- 19. SSRS HTML背景色を設定する
- 20. Android - SimpleCursorAdapter.ViewBinder - 背景色を設定する
- 21. リストビューで背景色を設定する
- 22. GXTのAggregationRowConfigでセルの背景色を設定するには
- 23. WPFのボタンの背景色を設定するには
- 24. cocos2dのCCLabelTTFの背景色を設定するには
- 25. TableRow内のTableカラムの背景色を設定するには
- 26. 角のmd-toolbarの背景色を設定するには
- 27. Canvas3Dの背景色を設定するには?
- 28. ラベルの背景色を設定するには
- 29. Objective cのMAC OSXで背景色を設定するには
- 30. テーブルビューの背景色を設定するには?
「NSAttributedString」と「NSBackgroundAttributeName」があるかもしれませんか? – Larme