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;
    }
    
    
    • 效果如下
  • 相关阅读:
    为什么使用Redis
    [Apache Pulsar] 企业级分布式消息系统-Pulsar快速上手
    [Apache Pulsar] 企业级分布式消息系统-Pulsar入门基础
    JDK源码分析系列---ArrayList和LinkList
    JDK源码分析系列---String,StringBuilder,StringBuffer
    单点登录
    单例模式的几种实现方式
    Bloom’S Taxonomy
    Python 字符串多替换时性能基准测试
    软件开发的生产力vs质量
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6116319.html
Copyright © 2011-2022 走看看