swift 如何在 UIButton 上包装文本
How to wrap a text on a UIButton in swift
我尝试按如下方式在按钮上包装文本:
nextButton=UIButton(frame: CGRectMake(buttonHWidth, textHeigth, buttonHWidth, buttonHeigth));
nextButton.backgroundColor = UIColor.lightGrayColor()
nextButton.setTitle("", forState: UIControlState.Normal)
nextButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
nextButton.tag = 22;
label_nextButton = UILabel(frame: CGRectMake(buttonHWidth, textHeigth, buttonHWidth, buttonHeigth));
label_nextButton.textAlignment = NSTextAlignment.Center;
label_nextButton.numberOfLines = 2;
label_nextButton.font = UIFont.systemFontOfSize(16.0);
label_nextButton.text = "Prss next Press next";
label_nextButton.textColor=UIColor.blackColor();
nextButton.addSubview(label_nextButton);
self.view.addSubview(nextButton);
我可以看到设备上的按钮,但看不到任何文字。
我做错了什么?或者这可以在不向按钮添加标签的情况下完成吗?
感谢您的帮助。
插图。看起来像:
刚刚做的时候:
nextButton.setTitle("this is a very very long text", forState: UIControlState.Normal)
您需要在setTitle方法中写入文字:
nextButton.setTitle("This is the very very long text!", forState: UIControlState.Normal)
设置标题标签的行数和换行模式:
nextButton.titleLabel.numberOfLines = 0; // Dynamic number of lines
nextButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
Update to Swift 4
nextButton.titleLabel.numberOfLines = 0; // Dynamic number of lines
nextButton.titleLabel.lineBreakMode = NSLineBreakMode.byWordWrapping;
Swift 4
要通过用“...”替换文本中间来缩短文本,您应该使用 NSLineBreakMode.byWordWrapping:
nextButton.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
要通过将文本末尾替换为“...”来缩短文本,您应该使用 NSLineBreakMode.byTruncatingTail:
nextButton.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
要查看可用包装模式的完整列表,请查看 NSLineBreakMode 文档:https://developer.apple.com/documentation/appkit/nslinebreakmode
我尝试按如下方式在按钮上包装文本:
nextButton=UIButton(frame: CGRectMake(buttonHWidth, textHeigth, buttonHWidth, buttonHeigth));
nextButton.backgroundColor = UIColor.lightGrayColor()
nextButton.setTitle("", forState: UIControlState.Normal)
nextButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
nextButton.tag = 22;
label_nextButton = UILabel(frame: CGRectMake(buttonHWidth, textHeigth, buttonHWidth, buttonHeigth));
label_nextButton.textAlignment = NSTextAlignment.Center;
label_nextButton.numberOfLines = 2;
label_nextButton.font = UIFont.systemFontOfSize(16.0);
label_nextButton.text = "Prss next Press next";
label_nextButton.textColor=UIColor.blackColor();
nextButton.addSubview(label_nextButton);
self.view.addSubview(nextButton);
我可以看到设备上的按钮,但看不到任何文字。
我做错了什么?或者这可以在不向按钮添加标签的情况下完成吗? 感谢您的帮助。
插图。看起来像:
刚刚做的时候:
nextButton.setTitle("this is a very very long text", forState: UIControlState.Normal)
您需要在setTitle方法中写入文字:
nextButton.setTitle("This is the very very long text!", forState: UIControlState.Normal)
设置标题标签的行数和换行模式:
nextButton.titleLabel.numberOfLines = 0; // Dynamic number of lines
nextButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
Update to Swift 4
nextButton.titleLabel.numberOfLines = 0; // Dynamic number of lines
nextButton.titleLabel.lineBreakMode = NSLineBreakMode.byWordWrapping;
Swift 4
要通过用“...”替换文本中间来缩短文本,您应该使用 NSLineBreakMode.byWordWrapping:
nextButton.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
要通过将文本末尾替换为“...”来缩短文本,您应该使用 NSLineBreakMode.byTruncatingTail:
nextButton.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
要查看可用包装模式的完整列表,请查看 NSLineBreakMode 文档:https://developer.apple.com/documentation/appkit/nslinebreakmode