Sco*_*ott 86 uibutton ios swift
我想知道是否可以用两行文本创建一个UIButton.我需要每一行都有不同的字体大小.第一行是17点,第二行是11点.我已经尝试过将两个标签放在UIButton中,但是我不能让它们留在按钮的范围内.
我试图在ui构建器中完成所有这些操作,而不是以编程方式完成.
谢谢
Sha*_* TK 211
有两个问题.
我想知道是否可以用两行文本创建一个UIButton
这可以通过使用故事板或以编程方式实现.
故事板:
将" 换行模式"更改为字符换行或自动换行,并使用Alt/Option + Enter键在UIButton的"标题"字段中输入新行.

编程方式:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
}
Run Code Online (Sandbox Code Playgroud)
我需要每一行都有不同的字体大小1
最糟糕的情况是,您可以使用自定义UIButton类并在其中添加两个标签.
更好的方法是,利用NSMutableAttributedString.请注意,这只能通过编程方式实现.
@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: [])
}
Run Code Online (Sandbox Code Playgroud)
产量

Nic*_* S. 20
我正在寻找几乎相同的主题,除了我不需要两种不同的字体大小.如果有人正在寻找一个简单的解决方案:
let button = UIButton()
button.titleLabel?.numberOfLines = 0
button.titleLabel?.lineBreakMode = .byWordWrapping
button.setTitle("Foo\nBar", for: .normal)
button.titleLabel?.textAlignment = .center
button.sizeToFit()
button.addTarget(self, action: #selector(rightBarButtonTapped), for: .allEvents)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
Run Code Online (Sandbox Code Playgroud)
Mus*_*tri 12
我注意到大多数解决方案中存在的问题是在将换行模式设置为"字符换行"时,第二行将与第一行保持对齐
使所有线条居中.只需将标题从Plain更改为Attributed,然后您可以使每一行居中
SWIFT 3语法
let str = NSMutableAttributedString(string: "First line\nSecond Line")
str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 17), range: NSMakeRange(0, 10))
str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(11, 11))
button.setAttributedTitle(str, for: .normal)
Run Code Online (Sandbox Code Playgroud)
我已经解决了这个问题,我的解决方案只在故事板中。
变化:
它在Identity Inspector -> User Defined Runtime Attributes(这些 KeyPaths)中添加:
我在属性检查器中添加了这个:
| 归档时间: |
|
| 查看次数: |
56571 次 |
| 最近记录: |