简体   繁体   English

我们可以限制 NSMutableAttributedString 的宽度吗?

[英]Can we restrict the width of a NSMutableAttributedString?

I have a string Ex: Arya said "We must find a solution" yesterday.我有一个字符串 例如: Arya昨天“我们必须找到解决方案” Here I want to restrict the width of the text in bold ("We must find a solution") to 100, but others must not be shortened.这里我想将粗体文本(“我们必须找到解决方案”)的宽度限制为 100,但其他的不得缩短。 So I added it as 3 different NSMutableAttributedString s appended together and set this to a UILabel .所以我将它添加为 3 个不同的NSMutableAttributedString附加在一起并将其设置为UILabel I want to know if I can restrict the width of one of these strings alone.我想知道我是否可以单独限制这些字符串之一的宽度。 I tried the following :我尝试了以下方法:

let mutableString = "We must find a solution"

  1. mutableString.draw(in: CGRect(x: 0, y: 0, width: 100, height: 30))

  2. mutableString.draw(with: CGRect(x: 0, y: 0, width: 10, height: 10), options: .truncatesLastVisibleLine, context: .none)

  3. mutableString.boundingRect(with: CGSize(width: 100, height: 40), options: [.truncatesLastVisibleLine,.usesFontLeading], context: .none)

But none of them worked.但他们都没有工作。 I want to reproduce the UILabel .lineBreakMode = .byTruncatingTail for a NSMutableAttributedString .我想为NSMutableAttributedString重现UILabel .lineBreakMode = .byTruncatingTail Am I doing something wrong here, is there a way to achieve this?我在这里做错了什么,有没有办法实现这一目标?

Here is a solution using three UILabel s in a UIStackView :这是在UIStackView使用三个UILabel的解决方案:

override func viewDidLoad() {
    super.viewDidLoad()

    let label1 = UILabel()
    label1.text = "Arya said"
    label1.font = UIFont.italicSystemFont(ofSize: 20.0)

    let label2 = UILabel()        
    label2.text = "\"We must find a solution\""
    label2.font = UIFont.boldSystemFont(ofSize: 20.0)
    label2.lineBreakMode = .byTruncatingTail
    
    let label3 = UILabel()
    label3.text = "yesterday"
    label3.font = UIFont.italicSystemFont(ofSize: 20.0)
    
    let stack = UIStackView(arrangedSubviews: [label1, label2, label3])
    
    stack.axis = .horizontal
    stack.spacing = 4
    
    label2.translatesAutoresizingMaskIntoConstraints = false
    stack.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        label2.widthAnchor.constraint(equalToConstant: 100)
    ])
    
    self.view.addSubview(stack)
    
    NSLayoutConstraint.activate([
        stack.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
        stack.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
    ])
}

output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法更改NSMutableAttributedString的颜色 - Can't change color of NSMutableAttributedString NSMutableAttributedString无法正确设置属性或添加属性 - NSMutableAttributedString can't setAttributes or addAttributes correctly 我们可以仅在iPad 1中限制下载或启动通用应用程序吗? - Can we restrict download or launch of an universal app in iPad 1 alone? 无法从NSTextAttachment创建NSMutableAttributedString - Can't create NSMutableAttributedString from NSTextAttachment 我们可以限制某些网站在网络视图中打开吗 - Can we restrict some websites to open in web view iOS:更改NSMutableAttributedString的字体高度而不更改字体宽度? - iOS: Change NSMutableAttributedString's Font Height Without Changing Font Width? NSMutableAttributedString包含NSMutableAttributedString和NSString - NSMutableAttributedString contains NSMutableAttributedString and NSString 我们可以快速更改UIAlertController Buttons的高度和宽度吗? - Can we change the height and width of UIAlertController Buttons in swift 从视图中删除第三个按钮时如何增加两个按钮的宽度 - How we can increase width of two button when we remove third button from view NSMutableAttributedString不起作用 - NSMutableAttributedString not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM