使用NSAttributedString设置按钮的标题以及颜色。
示例代码如下:
NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:@"My Button Title" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
[myButton setAttributedTitle:titleString forState:UIControlStateNormal];
这段代码的作用是给按钮的标题设置了一个带有颜色属性的NSAttributedString对象,然后将该对象设置为按钮的标题。这样一来,按钮的标题文字颜色就能够被正确地显示出来了。