如何对齐字符串中的徽标
How to align a logo in a String
目前的代码允许我附加一个标志。像这样:
但是我怎样才能只对齐右边的 "Logo" 标志呢?
这就是我想要的:
let paragraph = NSMutableAttributedString()
let font = UIFont(name: "Helvetica Neue", size: 15.0) ?? UIFont.systemFontOfSize(18.0)
let align = NSTextAlignment.Center
let textFont = [
NSFontAttributeName : font]
let attrString1 = NSAttributedString(string: "\n Logo", attributes:textFont)
let attrString2 = NSAttributedString(attributedString: textview.attributedText)
paragraph.appendAttributedString(attrString2)
paragraph.appendAttributedString(attrString1)
let paraStyle = NSMutableParagraphStyle()
paraStyle.alignment = .Center
paraStyle.firstLineHeadIndent = 15.0
paraStyle.paragraphSpacingBefore = 3.0
paragraph.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location: 0,length: paragraph.length))
shareTextView.attributedText = paragraph
我认为您必须将对齐方式不同的 2 个部分设为不同的段落。
如果您希望不同部分采用不同的对齐方式,请为两种段落样式指定不同的范围。因此,您可以执行以下操作:
let centerStyle = NSMutableParagraphStyle()
centerStyle.alignment = .Center
let rightStyle = NSMutableParagraphStyle()
rightStyle.alignment = .Right
let font = UIFont(name: "Helvetica Neue", size: 24.0) ?? UIFont.systemFontOfSize(24.0)
let fontAttributes = [NSFontAttributeName : font]
let attributedText = NSMutableAttributedString(string: "Foo\nBar", attributes: fontAttributes)
attributedText.addAttribute(NSParagraphStyleAttributeName, value: centerStyle, range: NSRange(location: 0, length: 4))
attributedText.addAttribute(NSParagraphStyleAttributeName, value: rightStyle, range: NSRange(location: 4, length: 3))
textView.attributedText = attributedText
或者,您可以更简单地附加具有不同属性的属性字符串:
let font = UIFont(name: "Helvetica Neue", size: 24.0) ?? UIFont.systemFontOfSize(24.0)
let centerAttributes = [NSFontAttributeName : font, NSParagraphStyleAttributeName : centerStyle]
let attributedText = NSMutableAttributedString(string: "Foo\n", attributes: centerAttributes)
let rightAttributes = [NSFontAttributeName : font, NSParagraphStyleAttributeName : rightStyle]
attributedText.appendAttributedString(NSMutableAttributedString(string: "Bar", attributes: rightAttributes))
textView.attributedText = attributedText
结果:
目前的代码允许我附加一个标志。像这样:
但是我怎样才能只对齐右边的 "Logo" 标志呢?
这就是我想要的:
let paragraph = NSMutableAttributedString()
let font = UIFont(name: "Helvetica Neue", size: 15.0) ?? UIFont.systemFontOfSize(18.0)
let align = NSTextAlignment.Center
let textFont = [
NSFontAttributeName : font]
let attrString1 = NSAttributedString(string: "\n Logo", attributes:textFont)
let attrString2 = NSAttributedString(attributedString: textview.attributedText)
paragraph.appendAttributedString(attrString2)
paragraph.appendAttributedString(attrString1)
let paraStyle = NSMutableParagraphStyle()
paraStyle.alignment = .Center
paraStyle.firstLineHeadIndent = 15.0
paraStyle.paragraphSpacingBefore = 3.0
paragraph.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location: 0,length: paragraph.length))
shareTextView.attributedText = paragraph
我认为您必须将对齐方式不同的 2 个部分设为不同的段落。
如果您希望不同部分采用不同的对齐方式,请为两种段落样式指定不同的范围。因此,您可以执行以下操作:
let centerStyle = NSMutableParagraphStyle()
centerStyle.alignment = .Center
let rightStyle = NSMutableParagraphStyle()
rightStyle.alignment = .Right
let font = UIFont(name: "Helvetica Neue", size: 24.0) ?? UIFont.systemFontOfSize(24.0)
let fontAttributes = [NSFontAttributeName : font]
let attributedText = NSMutableAttributedString(string: "Foo\nBar", attributes: fontAttributes)
attributedText.addAttribute(NSParagraphStyleAttributeName, value: centerStyle, range: NSRange(location: 0, length: 4))
attributedText.addAttribute(NSParagraphStyleAttributeName, value: rightStyle, range: NSRange(location: 4, length: 3))
textView.attributedText = attributedText
或者,您可以更简单地附加具有不同属性的属性字符串:
let font = UIFont(name: "Helvetica Neue", size: 24.0) ?? UIFont.systemFontOfSize(24.0)
let centerAttributes = [NSFontAttributeName : font, NSParagraphStyleAttributeName : centerStyle]
let attributedText = NSMutableAttributedString(string: "Foo\n", attributes: centerAttributes)
let rightAttributes = [NSFontAttributeName : font, NSParagraphStyleAttributeName : rightStyle]
attributedText.appendAttributedString(NSMutableAttributedString(string: "Bar", attributes: rightAttributes))
textView.attributedText = attributedText
结果: