Ios 长的UIButton标题标签在中间而不是右侧截断
我正在测试以下代码:Ios 长的UIButton标题标签在中间而不是右侧截断,ios,objective-c,uibutton,nsstring,ios8,Ios,Objective C,Uibutton,Nsstring,Ios8,我正在测试以下代码: username = @"addagkagalkjagjalggxxaklgjagjglkjag"; NSString *fullUsername = [NSString stringWithFormat:@"%@%@", @"@", username]; UIButton *usernameButton = [UIButton buttonWithType:UIButtonTypeSystem]; [usernameButton setTitle:fullUserna
username = @"addagkagalkjagjalggxxaklgjagjglkjag";
NSString *fullUsername = [NSString stringWithFormat:@"%@%@", @"@", username];
UIButton *usernameButton = [UIButton buttonWithType:UIButtonTypeSystem];
[usernameButton setTitle:fullUsername forState:UIControlStateNormal];
usernameButton.frame = CGRectMake(100.0, 10.0, 215.0, 25.0);
最终结果将截断标签中心的椭圆。我希望它在结尾,但我必须为此设置什么属性?对于Swift 5和iOS 12.3,
UILabel
有一个名为的属性lineBreakMode
具有以下声明:
usernameButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
var lineBreakMode: NSLineBreakMode { get set }
用于包装和截断标签文本的技术
下面的代码显示了如何使用
NSLineBreakMode设置lineBreakMode
。通过截断tail
值来截断右侧的ui按钮的标题标签:
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let button = UIButton(type: .system)
button.setTitle("Lorem ipsum dolor sit amet, consectetur adipiscing elit", for: .normal)
button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
}
PlaygroundPage.current.liveView = ViewController()