iOS Multiple Right and Left Align on Same Line

IOS Multiple right and left align on same line

try this

added a NSKernAttributeName after date

NSMutableAttributedString *descriptionAttribute = [[NSMutableAttributedString alloc] initWithString:@"hello how are you"];
NSMutableAttributedString *dateAttribute = [[NSMutableAttributedString alloc] initWithString:@"\nfinal datetime"];
NSMutableAttributedString *shareAttribute = [[NSMutableAttributedString alloc] initWithString:@"Share"];

NSMutableParagraphStyle *dateStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

[dateStyle setAlignment:NSTextAlignmentLeft];
NSMutableParagraphStyle *shareStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[shareStyle setAlignment:NSTextAlignmentRight];

[dateAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, dateAttribute.length)];
[dateAttribute addAttribute:NSParagraphStyleAttributeName value:dateStyle range:NSMakeRange(0, 13)];
[dateAttribute addAttribute:NSKernAttributeName value:@170 range:NSMakeRange(dateAttribute.length-1, 1)];

[shareAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 2)];
[shareAttribute addAttribute:NSParagraphStyleAttributeName value:shareStyle range:NSMakeRange(0, 5)];

[descriptionAttribute appendAttributedString:[dateAttribute mutableCopy]];

[descriptionAttribute appendAttributedString:[shareAttribute mutableCopy]];

theCell.detailTextLabel.attributedText = descriptionAttribute;

NSAttributedString with multiple alignments

Some of your NSRanges don't match the character offsets you seem to want to target.

Try this in a playground, it works for me:

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

Sample Image

align left and right in the same line swift

If I understand correctly, you want something like this:

func alignLeftAndRight(left: String, right: String, length: Int) -> String {
// calculate how many spaces are needed
let numberOfSpacesToAdd = length - left.count - right.count

// create those spaces
let spaces = Array(repeating: " ", count: numberOfSpacesToAdd < 0 ? 0 : numberOfSpacesToAdd).joined()

// join these three things together
return left + spaces + right
}

Usage:

print(alignLeftAndRight(left: "Product", right: "Price", length: 32))
print(alignLeftAndRight(left: "Foo", right: "1", length: 32))
print(alignLeftAndRight(left: "Product", right: "123", length: 32))
print(alignLeftAndRight(left: "Something", right: "44", length: 32))
print(alignLeftAndRight(left: "Hello", right: "7777", length: 32))

Output:

Product                    Price
Foo 1
Product 123
Something 44
Hello 7777

Two labels having first line in alignment

First approach ->
Use constraints give both labels the left, right, and top constraints only do not give bottom constraints, you need to be pro if you still want to provide bottom constraints :)

Second approach ->
Use UITextView and disable user interaction. this one is simple and easy and not messy and more customizable.

Left-align and right-align text in UITableViewCell's detailTextLabel

This worked for me. Try this:

 cell.detailTextLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping;
cell.detailTextLabel!.numberOfLines = 0;

If you want to set the left view and right view in the tableView.

Select your tableView and go to attribute inspector and set the prototype cell to 1 as shown in the screenshot below:

Sample Image

Select your tableView cell and go the attributes inspector and do the settings as shown in the screenshot below:

Sample Image

After selecting the right detail the tableview cell will consist as following screenshot:

Sample Image

The text set in "cell.textLabel!.text" is the left view (Title)
and the text set in the "cell.detailTextLabel!.text" is the right view (Detail)

Hope this might be helpful.

Attributed text with two text alignments

Using NSMutableParagraphStyle with NSTextTab:

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

screenshot

Of course, this aligns to "tabstop", but not to the edge of UITextView. When you modify the size of the view, you have to also modify the location of NSTextTab.



Related Topics



Leave a reply



Submit