我想知道是否有可能创建一个包含两行文本的UIButton.我需要每行有不同的字体大小.第一条线是17分,第二条线是11分.我try 过在一个UIButton中放置两个标签,但我无法让它们保持在按钮的范围内.

我试图在UI构建器中完成所有这些操作,而不是以编程方式.

谢谢

推荐答案

这里有两个问题.

我想知道是否有可能创建一个带有两行的UIButton

这可以通过使用故事板或编程实现.

Storyboard:

将"换行模式"更改为Character WrapWord Wrap,并使用Alt/Option + Enter键在UIButton的标题字段中输入新行.

在此处输入图像描述

Programmatically:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
}

我需要每行有不同的字体大小

最糟糕的情况是,您可以使用自定义的UIButton类,并在其中添加两个标签.

更好的办法是,利用NSMutableAttributedString.请注意,这只能通过编程实现.

Swift 5:

@IBOutlet weak var btnTwoLine: UIButton?

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    //applying the line break mode
    textResponseButton?.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping;
    let buttonText: NSString = "hello\nthere"

    //getting the range to separate the button title strings
    let newlineRange: NSRange = buttonText.range(of: "\n")

    //getting both substrings
    var substring1 = ""
    var substring2 = ""

    if(newlineRange.location != NSNotFound) {
        substring1 = buttonText.substring(to: newlineRange.location)
        substring2 = buttonText.substring(from: newlineRange.location)
    }

    //assigning diffrent fonts to both substrings
    let font1: UIFont = UIFont(name: "Arial", size: 17.0)!
    let attributes1 = [NSMutableAttributedString.Key.font: font1]
    let attrString1 = NSMutableAttributedString(string: substring1, attributes: attributes1)

    let font2: UIFont = UIFont(name: "Arial", size: 11.0)!
    let attributes2 = [NSMutableAttributedString.Key.font: font2]
    let attrString2 = NSMutableAttributedString(string: substring2, attributes: attributes2)

    //appending both attributed strings
    attrString1.append(attrString2)

    //assigning the resultant attributed strings to the button
    textResponseButton?.setAttributedTitle(attrString1, for: [])
}

Older Swift

@IBOutlet weak var btnTwoLine: UIButton?

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        //applying the line break mode
        btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;

        var buttonText: NSString = "hello\nthere"

        //getting the range to separate the button title strings
        var newlineRange: NSRange = buttonText.rangeOfString("\n")

        //getting both substrings
        var substring1: NSString = ""
        var substring2: NSString = ""

        if(newlineRange.location != NSNotFound) {
            substring1 = buttonText.substringToIndex(newlineRange.location)
            substring2 = buttonText.substringFromIndex(newlineRange.location)
        }

        //assigning diffrent fonts to both substrings
        let font:UIFont? = UIFont(name: "Arial", size: 17.0)
        let attrString = NSMutableAttributedString(
            string: substring1 as String,
            attributes: NSDictionary(
                object: font!,
                forKey: NSFontAttributeName) as [NSObject : AnyObject])

        let font1:UIFont? = UIFont(name: "Arial", size: 11.0)
        let attrString1 = NSMutableAttributedString(
            string: substring2 as String,
            attributes: NSDictionary(
                object: font1!,
                forKey: NSFontAttributeName) as [NSObject : AnyObject])

        //appending both attributed strings
        attrString.appendAttributedString(attrString1)

        //assigning the resultant attributed strings to the button
        btnTwoLine?.setAttributedTitle(attrString, forState: UIControlState.Normal)

    }

Output

在此处输入图像描述

Ios相关问答推荐

RxSwift:如何使用另外两个可观测对象(其中一个依赖于另一个)创建可观测对象?

objc[25442]:Swift类的扩展和类别不允许有+load方法

在 Swift 中,对象如何被准确地从内存中移除?

为什么 Firebase Messaging 配置对于 IOS native 和 Flutter iOS 不同?

动画文本位置

iOS SwiftUI - 使用 PhotosPicker 从图库中 Select 图像或视频

如何从 Locale Swift 获取货币符号

'DismissAction'类型的值没有成员'wrappedValue'在SwiftUI的Xcode中

如何仅更改一个视图的导航标题 colored颜色 ?

快速禁用 UITextfield 的用户输入

Swift 错误:在其自己的初始值中使用的变量

如何使用 swift 显示 .svg 图像

iOS 中是否有用于自定义振动的 API?

Xcode 故事板:内部错误.请提交错误

使用导航控制器在prepareForSegue中设置数据

是否可以使用 sharedHTTPCookieStorage 为 UIWebView 手动设置 cookie?

UIButton 事件.有什么不同?

Xcode:此时无法安装此应用程序.

Sierra 中的安全/协同设计: keys 串忽略访问控制设置和 UI 提示权限

Select 器touchesBegan:withEvent:的覆盖方法具有不兼容的类型(NSSet,UIEvent)->()