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;
    }
    
    
    • 效果如下
  • 相关阅读:
    k8s学习
    k8s学习
    k8s学习
    Linux 常用命令(持续补充)
    通过一个小故事,理解 HTTPS 工作原理
    Spring Cloud 微服务架构全链路实践
    Spring Cloud Eureka 使用 IP 地址进行服务注册
    RabbitMQ 消息顺序、消息幂等、消息重复、消息事务、集群
    Spring Boot 实现 RabbitMQ 延迟消费和延迟重试队列
    RabbitMQ 集群原理和完善
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6116319.html
Copyright © 2011-2022 走看看