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;
    }
    
    
    • 效果如下
  • 相关阅读:
    Spring在代码中获取bean的几种方式
    SpringBoot学习:获取yml和properties配置文件的内容(转)
    SpringBoot 使用 @Value 从 YAML文件读取属性(转)
    SpringBoot yml 配置
    Springboot+shiro配置笔记+错误小结
    shiro退出登陆清空缓存实现
    springboot(十四):springboot整合shiro-登录认证和权限管理(转)
    Apache Shiro 简单概念
    漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)
    Windows Vista 的历史地位
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6116319.html
Copyright © 2011-2022 走看看