質問

私はUILabelのテキストの色を変更したいが、私は色を変更することはできません、これはどのように私のコードを次のようになります。

UILabel *categoryTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 46, 16)];
categoryTitle.text = @"abc";
categoryTitle.backgroundColor = [UIColor clearColor];
categoryTitle.font = [UIFont systemFontOfSize:12];
categoryTitle.textAlignment = UITextAlignmentCenter;
categoryTitle.adjustsFontSizeToFitWidth = YES;
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];
[self.view addSubview:categoryTitle];
[categoryTitle release];

ラベルテキストの色はなく、私のカスタムカラー、白です。

任意の助けのためにありがとうございます。

役に立ちましたか?

解決

UIColorのRGB成分がない最大255、0と1の間でスケーリングされます。

試してみてください
categoryTitle.textColor = [UIColor colorWithRed:(188/255.f) green:... blue:... alpha:1.0];
<時間>

でスウィフトます:

categoryTitle.textColor = UIColor(red: 188/255.0, green: ..., blue: ..., alpha: 1)

他のヒント

より良い方法である。

かもしれませ
UIColor *color = [UIColor greenColor];
[self.myLabel setTextColor:color];

したがって、私たちが着色したテキスト

アルファは、不透明度などで、このいずれかを試してみてください赤、緑、青であるchannels-

self.statusTextLabel.textColor = [UIColor colorWithRed:(233/255.f) green:(138/255.f) blue:(36/255.f) alpha:1];
それは可能であるが、彼らはInterfaceBuilderに接続されていない。

テキストcolour(colorWithRed:(188/255) green:(149/255) blue:(88/255))が正しい、接続ミスであってもよいし、

背景色ラベルとtextcolorはの背景色がプロパティtextcolorはのために使用されるために使用されます。

SWIFTコードに起因するテキストの色を追加します。

スイフト4:

  let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
  let attributedStringColor = [NSAttributedStringKey.foregroundColor : greenColor];

  let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor)
  label.attributedText = attributedString
スウィフト3のための

  let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
  let attributedStringColor : NSDictionary = [NSForegroundColorAttributeName : greenColor];


  let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor as? [String : AnyObject])
  label.attributedText = attributedString 
// This is wrong 
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];

// This should be  
categoryTitle.textColor = [UIColor colorWithRed:188/255 green:149/255 blue:88/255 alpha:1.0];

// In the documentation, the limit of the parameters are mentioned.

colorWithRed:緑:青:アルファ:ドキュメントリンクする

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top