2013-12-19 100 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];