NSKernAttributeName space at end of line in an NSAttributedString

richy

When using NSKernAttributeName it puts a space at the end of each line, is there any way to fix this? I can set the attributed to be in the range of:

NSRange(location: 0, length: self.text!.characters.count-1)

But I don't want to set this for every line.

This is the test code in the a playground I am using

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

var text = "Hello, playground\nhow are you?"

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 50
paragraphStyle.alignment = NSTextAlignment.Left
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail

let attributes = [
    NSParagraphStyleAttributeName: paragraphStyle
    , NSKernAttributeName: 20
]


let attributedString = NSAttributedString(string: text, attributes: attributes)

let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 0
label.textColor = UIColor.greenColor()
label.backgroundColor = UIColor.orangeColor()
label.sizeToFit()
label.center = CGPoint(x: 500, y: 100)


var text2 = "What's up\nWhere are you?"
let attributedString2 = NSAttributedString(string: text2, attributes: attributes)

let label2 = UILabel()
label2.attributedText = attributedString2
label2.numberOfLines = 0
label2.textColor = UIColor.greenColor()
label2.backgroundColor = UIColor.orangeColor()
label2.sizeToFit()
label2.center = CGPoint(x: 500, y: 250)

var text3 = "Hello"
let attributedString3 = NSAttributedString(string: text3, attributes: attributes)

let label3 = UILabel()
label3.attributedText = attributedString3
label3.numberOfLines = 0
label3.textColor = UIColor.greenColor()
label3.backgroundColor = UIColor.orangeColor()
label3.sizeToFit()
label3.center = CGPoint(x: 500, y: 400)

let holderView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 500))
holderView.backgroundColor = UIColor.lightGrayColor()
holderView.addSubview(label)
holderView.addSubview(label2)
holderView.addSubview(label3)

XCPlaygroundPage.currentPage.liveView = holderView

With the result looking like this:

text with kerning at the end of a line

You can see the spaces at the end of each of the lines.

Alex Curylo

This is actually the definition of how kerning works; it adjusts the space between the kerned character and where the next character will be. Whether a next character proceeds to be drawn or not is irrelevant.

Standard Attributes

The kerning attribute indicates how much the following character should be shifted from its default offset as defined by the current character’s font; a positive kern indicates a shift farther along and a negative kern indicates a shift closer to the current character.

If it helps, think about doing this in a word processor. If kerning is on, and you type a character, where would you expect the insertion point to be then? The expected answer would be "offset from the just typed character by the amount of kern" as that's what happens in the default case of kern being 0, correct? Well, that's exactly what's happening here: if you kern the last character of a string, the string therefore includes the last kern.

So the correct thing to do here is to wrap up your dropLast() logic in an extension and call it a day.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NSKernAttributeName space at end of line in an NSAttributedString

From Dev

NSAttributedString end of first line indent

From Dev

Non breaking space at the end of the line

From Dev

Strip first space to end of line

From Dev

Strip first space to end of line

From Dev

Paste text at the end of the line with a space between in vim

From Dev

remove white space from the end of line in linux

From Dev

End of NSAttributedString with emojis left unformatted

From Dev

NSAttributedString change color at end of string

From Dev

Remove Space at end of String but keep new line symbol

From Dev

attempting to add cost from the end of a line but only getting a blank space

From Dev

sed remove last space only on end of line,combined with awk

From Dev

Replace each character of white space at the end of each line with '_'

From Dev

Gnu Sed: Space Character class not matching for end of line character

From Dev

Upgrading to Powerlevel10k - Extra space at the end of line

From Dev

printing a number pattern in python without white space and new line at the end

From Dev

Draw a line inside a UITextView - NSAttributedString

From Dev

Preventing line breaks in part of an NSAttributedString

From Dev

Last line of NSAttributedString not rendered in UILabel

From Dev

Last line of NSAttributedString not rendered in UILabel

From Dev

NSAttributedString with right alignment removes whitespace at the end

From Dev

NSAttributedString with right alignment removes whitespace at the end

From Dev

Random space at the end of entry that generates new line in a text file python tkinter

From Dev

Is it a way to fill up collectionView lines from the beginning of the line and leave space in the end?

From Dev

How can I have a long line under icon and editText and also leave some space at the end of editText?

From Dev

Remove Extra Space At End

From Dev

Add space to the end of the RecyclerView

From Dev

Empty space at the end of page

From Dev

Unallocated space at the end of HDDs

Related Related

  1. 1

    NSKernAttributeName space at end of line in an NSAttributedString

  2. 2

    NSAttributedString end of first line indent

  3. 3

    Non breaking space at the end of the line

  4. 4

    Strip first space to end of line

  5. 5

    Strip first space to end of line

  6. 6

    Paste text at the end of the line with a space between in vim

  7. 7

    remove white space from the end of line in linux

  8. 8

    End of NSAttributedString with emojis left unformatted

  9. 9

    NSAttributedString change color at end of string

  10. 10

    Remove Space at end of String but keep new line symbol

  11. 11

    attempting to add cost from the end of a line but only getting a blank space

  12. 12

    sed remove last space only on end of line,combined with awk

  13. 13

    Replace each character of white space at the end of each line with '_'

  14. 14

    Gnu Sed: Space Character class not matching for end of line character

  15. 15

    Upgrading to Powerlevel10k - Extra space at the end of line

  16. 16

    printing a number pattern in python without white space and new line at the end

  17. 17

    Draw a line inside a UITextView - NSAttributedString

  18. 18

    Preventing line breaks in part of an NSAttributedString

  19. 19

    Last line of NSAttributedString not rendered in UILabel

  20. 20

    Last line of NSAttributedString not rendered in UILabel

  21. 21

    NSAttributedString with right alignment removes whitespace at the end

  22. 22

    NSAttributedString with right alignment removes whitespace at the end

  23. 23

    Random space at the end of entry that generates new line in a text file python tkinter

  24. 24

    Is it a way to fill up collectionView lines from the beginning of the line and leave space in the end?

  25. 25

    How can I have a long line under icon and editText and also leave some space at the end of editText?

  26. 26

    Remove Extra Space At End

  27. 27

    Add space to the end of the RecyclerView

  28. 28

    Empty space at the end of page

  29. 29

    Unallocated space at the end of HDDs

HotTag

Archive