按钮标题颜色

时间:2013-12-19 10:39:43

标签: ios iphone uibutton

我想设置带下划线和褐色的按钮标题:

我正在做的事情:

 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];

显示下划线但不是褐色。

3 个答案:

答案 0 :(得分:1)

尝试像这样使用....

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

答案 1 :(得分:1)

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

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];

答案 2 :(得分:0)

    NSString *tempString=@"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];
相关问题