【问题标题】:Attributed text with two text alignments具有两个文本对齐方式的属性文本
【发布时间】:2016-01-06 21:20:45
【问题描述】:

有谁知道如何在一个字符串中实现两种不同的文本对齐方式?

这就是我希望 textView 显示的内容:

label                                                                         value

我的代码:

let txtView = cell.viewWithTag(77) as! UITextView

let leftStyle = NSMutableParagraphStyle()
leftStyle.alignment = NSTextAlignment.Left
let rightStyle = NSMutableParagraphStyle()
rightStyle.alignment = NSTextAlignment.Right

let attText = NSMutableAttributedString(string: "label", attributes: [NSParagraphStyleAttributeName: leftStyle])
attText.appendAttributedString(NSAttributedString(string: " "))
attText.appendAttributedString(NSAttributedString(string: "value", attributes: [NSParagraphStyleAttributeName: rightStyle]))

txtView.attributedText = attText

我得到的却是:

label value

【问题讨论】:

  • 取一个UIVIew并设置两个UILabel,一个左对齐,另一个右对齐。

标签: ios swift uitextview nsattributedstring


【解决方案1】:

NSMutableParagraphStyleNSTextTab 一起使用:

let paragraph = NSMutableParagraphStyle()
paragraph.tabStops = [
    NSTextTab(textAlignment: .Right, location: 100, options: [:]),
]

let str = "Label\tValue\n"
    + "foo\tbar\n"

let attributed = NSAttributedString(
    string: str,
    attributes: [NSParagraphStyleAttributeName: paragraph]
)

let view = UITextView(frame: CGRectMake(0, 0, 120, 120))
view.textContainer.lineFragmentPadding = 10

view.attributedText = attributed

当然,这与“制表位”对齐,但不会与UITextView 的边缘对齐。修改视图大小的时候,还要修改locationNSTextTab

【讨论】:

  • 太棒了。谢谢! :)
  • @rintaro - 你为什么要做位置:100? 100 是什么意思?
  • @SamB 100 是view.textContainer.size.width - view.textContainer.lineFragmentPadding * 2。这意味着文本区域的右边缘。
【解决方案2】:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        let cell = Placestableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath);

        //first text
        let attributestitle = [NSAttributedStringKey.font:
            UIFont(name: "Helvetica-Bold", size: 15.0)!,
                          NSAttributedStringKey.foregroundColor: UIColor.black] as [NSAttributedStringKey: Any]

        //second text
        let attributedString = NSMutableAttributedString(string: "\t"+String(places[indexPath.row].distance!)+" miles", attributes: [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 8.0)!,NSAttributedStringKey.foregroundColor: UIColor.black])


        let myParagraphStyle = NSMutableParagraphStyle()
        myParagraphStyle.alignment = .right
        myParagraphStyle.tabStops = [
            NSTextTab(textAlignment: .right, location: 300, options: [:]),
        ]

        let attributedStringtitle  = NSMutableAttributedString(string: places[indexPath.row].title!, attributes: attributestitle)
        //adding the right alignment to the second text alone
        attributedString.addAttributes([.paragraphStyle: myParagraphStyle], range: NSRange(location: 0, length: attributedString.length))

        //combining two texts with different alignments.
        let combination = NSMutableAttributedString()
        combination.append(attributedStringtitle)
        combination.append(attributedString)
        cell.textLabel?.attributedText = combination
        return cell;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多