【问题标题】:Setting the font of titleLabel of UIButton changes the textColor to default one?设置 UIButton 的 titleLabel 字体会将 textColor 更改为默认字体?
【发布时间】:2014-01-28 15:13:35
【问题描述】:

我现在正在为 ios 6 开发应用程序。这是一种表单设计应用程序,我发现如果我们在应用程序中的某个点更改titlelabel.textColor,那么如果我们更改字体,那么 textColor 会在分配时更改为默认值。 我像下面这样检查了这个

我将一个示例按钮拖到我的示例视图控制器并在 viewcontroller.h 文件中创建一个名为 myButton 的 IBOutlet 属性 在 -viewDidLoad

[myButton.titleLabel setTextColor:[UIColor redColor]];

在按钮操作中我喜欢下面

- (IBAction)btnLoginClicke:(UIButton *)sender {
   [myButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:18]];

以下是我点击按钮前后的动作截图

【问题讨论】:

    标签: ios uibutton


    【解决方案1】:

    如果您要让应用程序中的所有按钮保持相似的颜色,那么最好使用全局外观设置。那么你就不用担心所有地方的按钮标题颜色了。

    将这些行放在appDelegate 中的didFinishLaunchWithOptions 方法中。

    [[UIButton appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [[UIButton appearance] setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      如果您使用以下方法设置 UIButton 标题的颜色:

      [myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
      

      分配新字体时颜色信息不会丢失。

      我认为这是因为 UIButton 类在内部根据您使用 setTitleColor(或情节提要)为不同的 UIButton 状态设置的不同颜色来设置 textLabel 的颜色。直接更改 textLabel 的颜色不会更改 UIButton 为这些状态中的每一个存储的颜色,并且每次 UIButton 需要重绘自身时都会重新应用它们。这就是设置新字体时发生的情况 - UIButton 必须重绘自己,但它会使用通过 setTitleColor:forState: 设置的颜色来完成。

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 1970-01-01
        • 2022-11-19
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多