[英]A long UIButton title label truncates in the center instead of on the right
我正在測試以下代碼:
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);
最終結果在標簽的中心截斷和省略。 我最后想要它,但我必須為此設置什么屬性?
usernameButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
使用Swift 5和iOS 12.3, UILabel
有一個名為lineBreakMode
的屬性。 lineBreakMode
具有以下聲明:
var lineBreakMode: NSLineBreakMode { get set }
用於包裝和截斷標簽文本的技術。
下面的Playground代碼顯示了如何使用NSLineBreakMode.byTruncatingTail
值設置lineBreakMode
以截斷右側的UIButton
的標題標簽:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.