代码改变世界

iOS - UIButton折行文字显示设置

2016-11-30 09:24  菜鸟Alex  阅读(3403)  评论(0编辑  收藏  举报

首先在控制器中创建一个button


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 35, 50)];
    [self.view addSubview:button];
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    /*
     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 setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
    button.layer.borderColor = [UIColor blackColor].CGColor;
    button.layer.borderWidth = 1.0;
}

  • 此处宽度故意设置的比较小由于文字过长,则设置button.titleLabel.lineBreakMode的属性为NSLineBreakByTruncatingHead时,此时button的title显示效果如下

  • 显示的前端省略而且只显示了内容line1,line2被省略掉,当把\n换行符去掉时,则line2不会被省略.

  • 此处宽度故意设置的比较小由于文字过长,则设置button.titleLabel.lineBreakMode的属性为NSLineBreakByTruncatingTail时,此时button的title显示效果如下

  • 省略号在后面

  • 此处宽度故意设置的比较小由于文字过长,则设置button.titleLabel.lineBreakMode的属性为NSLineBreakByTruncatingMiddle时,此时button的title显示效果如下

  • 省略号在中间

  • 此处宽度故意设置的比较小由于文字过长,则设置button.titleLabel.lineBreakMode的属性为NSLineBreakByClipping时,此时button的title显示效果如下

  • 超长部分被裁剪掉

把宽度设置宽一些让button的文字title折行显示


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 50, 50)];
    [self.view addSubview:button];
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    /*
     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 setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
    button.layer.borderColor = [UIColor blackColor].CGColor;
    button.layer.borderWidth = 1.0;
}

  • 效果如下