UIButton在Objective c中划线

时间:2015-02-19 09:22:50

标签: ios objective-c iphone uibutton

正常下划线工作,但虚线下划线没有#39; t似乎有用吗?

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
btn.frame = CGRectMake(100, 10, 300, 300);
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"Durga Vundavalli"];

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

// using text on button
[btn setAttributedTitle: titleString forState:UIControlStateNormal];
[self.view addSubview:btn];

以下是文档中的枚举:

typedef NS_ENUM(NSInteger, NSUnderlineStyle) {
    NSUnderlineStyleNone                                = 0x00,
    NSUnderlineStyleSingle                              = 0x01,
    NSUnderlineStyleThick NS_ENUM_AVAILABLE_IOS(7_0)    = 0x02,
    NSUnderlineStyleDouble NS_ENUM_AVAILABLE_IOS(7_0)   = 0x09,

    NSUnderlinePatternSolid NS_ENUM_AVAILABLE_IOS(7_0)      = 0x0000,
    NSUnderlinePatternDot NS_ENUM_AVAILABLE_IOS(7_0)        = 0x0100,
    NSUnderlinePatternDash NS_ENUM_AVAILABLE_IOS(7_0)       = 0x0200,
    NSUnderlinePatternDashDot NS_ENUM_AVAILABLE_IOS(7_0)    = 0x0300,
    NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE_IOS(7_0) = 0x0400,

    NSUnderlineByWord NS_ENUM_AVAILABLE_IOS(7_0) = 0x8000
}

1 个答案:

答案 0 :(得分:13)

您不能只放NSUnderlinePatternDot。如果你想要2行点怎么办?您必须使用PatternStyle

[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:(NSUnderlinePatternDot|NSUnderlineStyleSingle)] range:NSMakeRange(0, [titleString length])];

你必须使用掩码,如doc:

所述
  

讨论
  样式,模式和可选的单词掩码是OR'd   一起产生NSUnderlineStyleAttributeNameNSStrikethroughStyleAttributeName的价值   {{1}}。