iOS - UIButton设置文字标题下划线以及下划线颜色
2016-11-30 09:59 菜鸟Alex 阅读(18153) 评论(0) 编辑 收藏 举报创建button设置可以折行显示
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 150, 70)];
[self.view addSubview:button];
[button setTitle:@"button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//button 折行显示设置
/*
NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default
NSLineBreakByCharWrapping, // Wrap at character boundaries
NSLineBreakByClipping, // Simply clip 裁剪从前面到后面显示多余的直接裁剪掉
文字过长 button宽度不够时: 省略号显示位置...
NSLineBreakByTruncatingHead, // Truncate at head of line: "...wxyz" 前面显示
NSLineBreakByTruncatingTail, // Truncate at tail of line: "abcd..." 后面显示
NSLineBreakByTruncatingMiddle // Truncate middle of line: "ab...yz" 中间显示省略号
*/
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 1.0;
// underline Terms and condidtions
NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"];
//设置下划线...
/*
NSUnderlineStyleNone = 0x00, 无下划线
NSUnderlineStyleSingle = 0x01, 单行下划线
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02, 粗的下划线
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09, 双下划线
*/
[tncString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:(NSRange){0,[tncString length]}];
//此时如果设置字体颜色要这样
[tncString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,[tncString length])];
//设置下划线颜色...
[tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:(NSRange){0,[tncString length]}];
[button setAttributedTitle:tncString forState:UIControlStateNormal];
}
- 设置button的下划线直接设置文字的属性
NSMutableAttributedString
的tncString
添加文字属性NSUnderlineStyleAttributeName
并设置下划线样式,NSUnderlineStyleNone
-- 无下划线,NSUnderlineStyleSingle
-- 单行下划线,NSUnderlineStyleThick
-- 单行加粗下划线,NSUnderlineStyleDouble
-- 双下划线. - 设置下划线颜色属性
NSUnderlineColorAttributeName
- 最主要的是设置button的标题为
NSMutableAttributedString
包含多种属性的字符串string.
UILabel下划线设置: http://www.cnblogs.com/adampei-bobo/p/6500782.html