Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 长的UIButton标题标签在中间而不是右侧截断_Ios_Objective C_Uibutton_Nsstring_Ios8 - Fatal编程技术网

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()