UITextView 拒绝更改其颜色

发布于 2024-08-26 04:25:26 字数 605 浏览 4 评论 0原文

在某些情况下,您希望在一秒钟内解决的事情最终会成为一生的冒险。这是其中一种情况:)

我想做的只是更改我的 UITextView 之一的文本颜色。到目前为止,我尝试过:

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

或者

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

两者似乎都适用于 UILabels,但不适用于 UITextView 元素。当我尝试 [UIColor colorWithHue... 我只得到一种略带红色的颜色,无论我选择什么值(黑色和白色的值除外。它们有效)。 colorWithPatternImage 根本不改变textColor。

很奇怪不是吗?显然我一定错过了一些东西。非常感谢您的帮助。提前致谢。

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. 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. 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). The colorWithPatternImage does not change textColor at all.

Strange isn't it? I obviously must be missing something. Help is very much appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰之心 2024-09-02 04:25:26

+colorWithHue:saturation:brightness:alpha: 与其他 UIColor 方法一样,采用 0 到 1 之间的值,而不是 0 到 100。我不希望 + colorWithPatternImage: 完全可以处理文本渲染,除非您想制作自己的基于 Core Graphics 的绘图代码。

+colorWithHue:saturation:brightness:alpha:, like the other UIColor methods, takes values from 0 to 1, not 0 to 100. I would not expect +colorWithPatternImage: to work with text rendering at all, unless you feel like making your own Core Graphics-based drawing code.

茶色山野 2024-09-02 04:25:26

正如 Noah 所说,值预计在 [0, 1] 范围内。请尝试此操作并根据需要进行修改:

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

As Noah said, values are expected to be in the range [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];
情痴 2024-09-02 04:25:26

如果你习惯了图形程序的颜色数字,并且想为了可读性而牺牲一点效率,你可以在下面的赋值语句中进行转换,如下面的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

你可以表示将色调数字作为分数从 Fireworks 色调、饱和度、亮度数字转换为 UIColor 的 0-1 比例。它对 Hue 特别有用,因为 Fireworks 使用 360 度刻度。 Fireworks 中饱和度、亮度和 alpha 都除以 100,因此转换很简单。在开发过程中,Hue 分数将我的图形程序呈现颜色 HSB 的方式与 Objective-C 所需的方式联系起来。这些数字可以很容易地调整。对于发布版本,我可以用 0.54 代替 color1 的 112/360。

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:

    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. It's especially helpful on the Hue, for which Fireworks uses a 360 degree scale. The saturation, brightness, and alpha are divided by 100 in Fireworks, so the conversion is simple. In development, the Hue fraction bridges the way my graphics program presents color HSB, and what Objective-C requires. The numbers can be tweaked easily. For the release version, I could substitute .54 for color1's 112/360.

九命猫 2024-09-02 04:25:26

好的,我设法让它发挥作用。感谢您的帮助。

以防有人遇到色调值(以度为单位)的问题。在 XCode 中使用它之前,请将其除以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文