3

The currently code allow me to attach a logo. Like this:

But how can I align only the "Logo" sign on the right ? enter image description here

That is what I want: enter image description here

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
  • The "logo" need to be under the String –  Aug 06 '15 at 11:53
  • 2
    I don't understand your question at all. Do you have a logo image that you want to center with below the text? Or are you saying that you want the word "Logo" to have different horizontal alignment than the word "Example"? Can you post a picture of the effect you are after by creating it in a drawing program? – Duncan C Aug 06 '15 at 11:58
  • Question edited @DuncanC –  Aug 06 '15 at 12:01

2 Answers2

1

If you want different alignment for different portions, specify different ranges for the two paragraph styles. Thus, you can do something like:

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

Or, easier, you can append attributed strings with different attributes:

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

That yields:

enter image description here

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

I believe you have to make the 2 parts that have different alignments different paragraphs.

Duncan C
  • 128,072
  • 22
  • 173
  • 272