V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
lcl22hope
V2EX  ›  iDev

请问能否自定义 UIButton 的 titleLabel 呢,需求是想自定义 UILabel 中 text 显示不同颜色

  •  
  •   lcl22hope · 2015-04-11 12:54:02 +08:00 · 2828 次点击
    这是一个创建于 3324 天前的主题,其中的信息可能已经有所发展或是发生改变。
    google 后找到的各种资料都是 NSAttributeString 给 label 的 string 增加属性,然后在重新写 UILabel 的 drawRect 方法,请问有没有其他方式呢,多谢!
    5 条回复    2015-04-13 10:12:12 +08:00
    blankwonder
        1
    blankwonder  
       2015-04-11 12:56:38 +08:00
    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
    lcl22hope
        2
    lcl22hope  
    OP
       2015-04-11 12:58:56 +08:00
    @blankwonder 谢谢回答,这个方法我试过,它可以更改全部文字的颜色,但想只改变一部分,比如:备注1:hello world,“备注1:”部分想用绿色展示,但hello world用黑色
    qq2511296
        3
    qq2511296  
       2015-04-11 13:27:46 +08:00   ❤️ 2
    _label.text = @"备注1:hello world";
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:_label.text];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:[_label.text rangeOfString:@"备注1:"]];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[_label.text rangeOfString:@"hello world"]];
    _label.attributedText = attr;
    lcl22hope
        4
    lcl22hope  
    OP
       2015-04-11 14:39:15 +08:00
    @qq2511296 非常感谢,测试有效
    lzyroot
        5
    lzyroot  
       2015-04-13 10:12:12 +08:00
    @qq2511296 mark
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1107 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:05 · PVG 07:05 · LAX 16:05 · JFK 19:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.