简体   繁体   English

UITextView拒绝更改其颜色

[英]UITextView refuses to change its color

in some cases things you'd expect to solve within a sec turn out to become a lifetime adventure. 在某些情况下,您期望在几秒钟内解决的事情变成了终身冒险。 This is one of these cases :) 这是其中一种情况:)

All I wanted to do, is simply change the text color of one of my UITextViews. 我要做的只是更改UITextViews之一的文本颜色。 So far I tried: 到目前为止,我尝试了:

UIColor *myColor = [UIColor colorWithHue:38 saturation:98 brightness:100 alpha:1.0];
[myTextView setTextColor:myColor];

OR 要么

UIColor *myColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"colorImage.png"]];
[myTextView setTextColor:myColor]; 

Both seem to work fine for UILabels, but fail for UITextView elements. 两者似乎都可以很好地用于UILabel,但是不能用于UITextView元素。 When I try [UIColor colorWithHue... I only get a reddish kinda color, no matter what values I choose (except values for black and white. They work). 当我尝试[UIColor colorWithHue ...时,无论选择什么值(黑白值除外。它们都起作用),我只会得到带红色的颜色。 The colorWithPatternImage does not change textColor at all. colorWithPatternImage完全不更改textColor。

Strange isn't it? 是不是很奇怪 I obviously must be missing something. 我显然一定想念一些东西。 Help is very much appreciated. 非常感谢您的帮助。 Thanks in advance. 提前致谢。

像其他UIColor方法一样, +colorWithHue:saturation:brightness:alpha:取值范围是0到1,而不是0到100。我不希望+colorWithPatternImage:可以完全用于文本渲染,除非您想自己制作基于核心图形的绘图代码。

As Noah said, values are expected to be in the range [0, 1]. 如Noah所说,值应在[0,1]范围内。 Try this instead and modify as necessary: 尝试以下操作,并根据需要进行修改:

UIColor *myColor = [UIColor colorWithHue:0.38 saturation:0.98 brightness:1.0 alpha:1.0];
[myTextView setTextColor:myColor];

If you are used to your graphics program's color numbers, and want to sacrifice a little efficiency for readability, you can do the conversion in the assignment statements like colorWithHue:(112/360.0) below: 如果您习惯了图形程序的色号,并且希望牺牲一点效率来提高可读性,则可以在以下赋值语句(如colorWithHue:(112 / 360.0))中进行转换:

    UIColor *color1 = [UIColor colorWithHue:(112/360.0) saturation:.63 brightness:.73 alpha:1];//green
    UIColor *color2 = [UIColor colorWithHue:(70/360.0) saturation:.79 brightness:.84 alpha:1];//lime
    UIColor *color3 = [UIColor  colorWithHue:(49/360.0) saturation:.77 brightness:.91 alpha:1];//yellow

You can express the Hue numbers as fractions to translate from Fireworks Hue, Saturation, Brightness numbers to UIColor's 0-1 scale. 您可以将色相数字表示为分数,以将Fireworks色相,饱和度,亮度数字转换为UIColor的0-1比例。 It's especially helpful on the Hue, for which Fireworks uses a 360 degree scale. 这在Fireworks使用360度刻度的Hue上特别有用。 The saturation, brightness, and alpha are divided by 100 in Fireworks, so the conversion is simple. 在Fireworks中,饱和度,亮度和Alpha被100除,因此转换很简单。 In development, the Hue fraction bridges the way my graphics program presents color HSB, and what Objective-C requires. 在开发中,Hue分数桥接了我的图形程序呈现颜色HSB的方式以及Objective-C的要求。 The numbers can be tweaked easily. 可以轻松调整数字。 For the release version, I could substitute .54 for color1's 112/360. 对于发行版,我可以用.54代替color1的112/360。

Ok, I managed to get it to work. 好的,我设法使其正常工作。 Thanks for all your help. 感谢你的帮助。

In case somebody runs into problems with the hue value, which is in degrees. 如果有人遇到色相值问题(以度为单位)。 Devide it by 360 before you use it in XCode. 在XCode中使用它之前,请先进行360度的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM