zoukankan      html  css  js  c++  java
  • iOS

    首先在控制器中创建一个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
    Line2" forState: UIControlStateNormal];
        button.layer.borderColor = [UIColor blackColor].CGColor;
        button.layer.borderWidth = 1.0;
    }
    
    
    • 此处宽度故意设置的比较小由于文字过长,则设置button.titleLabel.lineBreakMode的属性为NSLineBreakByTruncatingHead时,此时button的title显示效果如下

    • 显示的前端省略而且只显示了内容line1,line2被省略掉,当把 换行符去掉时,则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
    Line2" forState: UIControlStateNormal];
        button.layer.borderColor = [UIColor blackColor].CGColor;
        button.layer.borderWidth = 1.0;
    }
    
    
    • 效果如下
  • 相关阅读:
    函数
    数值
    数据类型
    jQuery工具方法
    jQuery概述
    史上最全的SpringMVC学习笔记
    webpack-Hot Module Replacement(热更新)
    webpack-Manifest
    webpack-Targets(构建目标)
    webpack-Dependency Graph(依赖图)
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6116319.html
Copyright © 2011-2022 走看看