Swift - 带有两行文本的 UIButton

Swift - UIButton with two lines of text(Swift - 带有两行文本的 UIButton)
本文介绍了Swift - 带有两行文本的 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以创建一个带有两行文本的 UIButton.我需要每一行都有不同的字体大小.第一行将是 17 点,第二行将是 11 点.我尝试将两个标签放在 UIButton 内,但我无法让它们留在按钮的范围内.

我正在尝试在 ui 构建器中完成所有这些操作,而不是通过编程方式.

谢谢

解决方案

有两个问题.

<块引用>

我想知道是否可以用两行创建一个 UIButton文本

这可以通过使用情节提要或以编程方式实现.

故事板:

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

以编程方式:

覆盖 func viewDidAppear(animated: Bool) {super.viewDidAppear(动画)btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;}

<块引用>

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

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

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

斯威夫特 5:

@IBOutlet 弱变量 btnTwoLine: UIButton?覆盖 func viewDidAppear(动画:布尔){super.viewDidAppear(动画)//应用换行模式textResponseButton?.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping;let buttonText: NSString = "hello
there"//获取分隔按钮标题字符串的范围让 newlineRange: NSRange = buttonText.range(of: "
")//获取两个子字符串var substring1 = ""var substring2 = ""如果(newlineRange.location!= NSNotFound){substring1 = buttonText.substring(到:newlineRange.location)substring2 = buttonText.substring(来自:newlineRange.location)}//为两个子字符串分配不同的字体让 font1: UIFont = UIFont(name: "Arial", size: 17.0)!让 attributes1 = [NSMutableAttributedString.Key.font: font1]让 attrString1 = NSMutableAttributedString(string: substring1, attributes: attributes1)让 font2: UIFont = UIFont(name: "Arial", size: 11.0)!让 attributes2 = [NSMutableAttributedString.Key.font: font2]让 attrString2 = NSMutableAttributedString(string: substring2, attributes: attributes2)//附加两个属性字符串attrString1.append(attrString2)//将结果属性字符串分配给按钮textResponseButton?.setAttributedTitle(attrString1, for: [])}

老斯威夫特

@IBOutlet 弱变量 btnTwoLine: UIButton?覆盖 func viewDidAppear(动画:布尔){super.viewDidAppear(动画)//应用换行模式btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;var buttonText: NSString = "你好
there"//获取分隔按钮标题字符串的范围var newlineRange: NSRange = buttonText.rangeOfString("
")//获取两个子字符串var substring1: NSString = ""var substring2: NSString = ""如果(newlineRange.location!= NSNotFound){substring1 = buttonText.substringToIndex(newlineRange.location)substring2 = buttonText.substringFromIndex(newlineRange.location)}//为两个子字符串分配不同的字体让字体:UIFont?= UIFont(名称:Arial",大小:17.0)让 attrString = NSMutableAttributedString(字符串:substring1 作为字符串,属性:NSDictionary(对象:字体!,forKey: NSFontAttributeName) as [NSObject : AnyObject])让 font1:UIFont?= UIFont(名称:Arial",大小:11.0)让 attrString1 = NSMutableAttributedString(字符串:substring2 作为字符串,属性:NSDictionary(对象:font1!,forKey: NSFontAttributeName) as [NSObject : AnyObject])//附加两个属性字符串attrString.appendAttributedString(attrString1)//将结果属性字符串分配给按钮btnTwoLine?.setAttributedTitle(attrString, forState: UIControlState.Normal)}

输出

I was wondering if it is possible to create a UIButton with two lines of text. I need each line to have a different font size. The first line will be 17 point and the second will be 11 point. I've tried messing with putting two labels inside of a UIButton, but I can't get them to stay inside the bounds of the button.

I'm attempting to do all of this in the ui builder, and not programmatically.

Thanks

解决方案

There are two questions.

I was wondering if it is possible to create a UIButton with two lines of text

This is possible through using the storyboard or programmatically.

Storyboard:

Change the 'Line Break Mode' to Character Wrap or Word Wrap and use Alt/Option + Enter key to enter a new line in the UIButton's Title field.

Programmatically:

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

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

I need each line to have a different font size 1

The worst case is, you can use a custom UIButton class and add two labels within it.

The better way is, make use of NSMutableAttributedString. Note that,this can be achieved through only programmatically.

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
there"

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

    //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
there"

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

        //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

这篇关于Swift - 带有两行文本的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)