具有多个对齐方式的 NSAttributedString
NSAttributedString with multiple alignments
我有一个 UILabel,我希望其中的三个文本具有三种对齐方式:左对齐、居中对齐和右对齐。我找到了以下适用于左右对齐的解决方案,但我无法添加居中的解决方案:
text = "Pause\n09:30 - 11:30\nsome center text"
at = NSMutableAttributedString(string: text)
p1.alignment = .left
p2.alignment = .right
p3.alignment = .center
p2.paragraphSpacingBefore = -labelPause.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 4))
at.addAttribute(.paragraphStyle, value: p3, range: NSRange(location: 4, length: 6))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 6, length: 8))
labelPause.numberOfLines = 0
labelPause.attributedText = at
本应居中的文本恰好出现在下一行左对齐中。我做错了什么?
您的一些 NSRange
与您似乎想要定位的字符偏移量不匹配。
在操场上试试这个,它对我有用:
import UIKit
import PlaygroundSupport
let text = "Pause\n09:30 - 11:30\nsome centered text"
let at = NSMutableAttributedString(string: text)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 400, height: 60))
var p1 = NSMutableParagraphStyle()
var p2 = NSMutableParagraphStyle()
var p3 = NSMutableParagraphStyle()
p1.alignment = .left
p2.alignment = .right
p3.alignment = .center
p2.paragraphSpacingBefore = -label.font.lineHeight
p3.paragraphSpacingBefore = -label.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 5))
at.addAttribute(.paragraphStyle, value: p3, range: NSRange(location: 20, length: 6))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 6, length: 13))
label.numberOfLines = 0
label.attributedText = at
PlaygroundPage.current.liveView = label
我有一个 UILabel,我希望其中的三个文本具有三种对齐方式:左对齐、居中对齐和右对齐。我找到了以下适用于左右对齐的解决方案,但我无法添加居中的解决方案:
text = "Pause\n09:30 - 11:30\nsome center text"
at = NSMutableAttributedString(string: text)
p1.alignment = .left
p2.alignment = .right
p3.alignment = .center
p2.paragraphSpacingBefore = -labelPause.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 4))
at.addAttribute(.paragraphStyle, value: p3, range: NSRange(location: 4, length: 6))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 6, length: 8))
labelPause.numberOfLines = 0
labelPause.attributedText = at
本应居中的文本恰好出现在下一行左对齐中。我做错了什么?
您的一些 NSRange
与您似乎想要定位的字符偏移量不匹配。
在操场上试试这个,它对我有用:
import UIKit
import PlaygroundSupport
let text = "Pause\n09:30 - 11:30\nsome centered text"
let at = NSMutableAttributedString(string: text)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 400, height: 60))
var p1 = NSMutableParagraphStyle()
var p2 = NSMutableParagraphStyle()
var p3 = NSMutableParagraphStyle()
p1.alignment = .left
p2.alignment = .right
p3.alignment = .center
p2.paragraphSpacingBefore = -label.font.lineHeight
p3.paragraphSpacingBefore = -label.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 5))
at.addAttribute(.paragraphStyle, value: p3, range: NSRange(location: 20, length: 6))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 6, length: 13))
label.numberOfLines = 0
label.attributedText = at
PlaygroundPage.current.liveView = label