2013-12-19 64 views
0

我想设置按钮标题用下划线和browncolor的:按钮标题颜色

对于我做的:

NSMutableAttributedString *nearestLocation = [[NSMutableAttributedString alloc] initWithString:@"Locate Manually"]; 
[nearestLocation addAttribute:(NSString*)kCTUnderlineStyleAttributeName 
        value:[NSNumber numberWithInt:NSUnderlineStyleSingle] 
        range:(NSRange){0,[nearestLocation length]}]; 
[btn_manually setAttributedTitle:nearestLocation forState:UIControlStateNormal]; 
[btn_manually setTitleColor:[UIColor brownColor] forState:UIControlStateNormal]; 

它显示下划线,但不能browncolor。

+0

你需要使用NSMutableAttributedString喜欢你的下划线逻辑来设置颜色。 –

回答

1

尝试这样使用....

[nearestLocation addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(0,[nearestLocation length])]; 
1

在iOS中,NSAttributedString用于修改文本,可以使用“NSMutableAttributedString”多彩色文本,字体,样式等使用单一的UIButton或的UILabel。

NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"The Underlined text"]; 

// making text property to underline text- 
[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])]; 

// using text on button 
[btn_manually setAttributedTitle: titleString forState:UIControlStateNormal]; 
btn_manually.titleLabel.textColor = [UIColor brownColor]; 
0
NSString *[email protected]"your string"; 
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:tempString];                      
    int lenght=(int)[tempString length]; 
    [attString addAttribute:(id)NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(0,lenght)]; 
    [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, length)]; 

    UIButton *hyperLinkButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    hyperLinkButton.frame=CGRectMake(30, 5, 100, 30); 
    [hyperLinkButton setAttributedTitle:attString forState:UIControlStateNormal]; 
    hyperLinkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    hyperLinkButton.tag=[[[[[[personalDetailDictionary objectForKey:@"Acknowledgements"] objectForKey:@"details"] objectAtIndex:i] objectForKey:@"tagValue"] objectForKey:@"text"] intValue]; 
    [hyperLinkButton addTarget:self action:@selector(hyperLinkAction:) forControlEvents:UIControlEventTouchUpInside]; 
    hyperLinkButton.titleLabel.textAlignment=NSTextAlignmentLeft; 
    [self.view addSubview:hyperLinkButton];