IOS Multiple right and left align on same line - ios

I want to write on the same line of my tableview cell detailTextLabel.
For example
Left string right string.
I'm doing this :
NSMutableAttributedString *descriptionAttribute = [[NSMutableAttributedString alloc] initWithString:descriptionString];
NSMutableAttributedString *dateAttribute = [[NSMutableAttributedString alloc] initWithString:finalDate];
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, 13)];
[dateAttribute addAttribute:NSParagraphStyleAttributeName value:dateStyle range:NSMakeRange(0, 13)];
[shareAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 8)];
[shareAttribute addAttribute:NSParagraphStyleAttributeName value:shareStyle range:NSMakeRange(0, 5)];
[descriptionAttribute appendAttributedString:[dateAttribute mutableCopy]];
[descriptionAttribute appendAttributedString:[shareAttribute mutableCopy]];
myTableViewcell.detailTextLabel.attributedText = descriptionAttribute;
If I add a \n between date and share attribute string, the result is good.
But i want to have two string on the same line..
An idea ?
Thanks

It would be better to create a custom cell with 2 labels but if for some reason you still want to have it in a single label then below is a solution. If you know what is the height of the line then you can use paragraphSpacingBefore to do something like in code below. Note that it does not work when UILabel numberOfLines is set to 1 so you must set numberOfLines to for example 0:
let text = "left\nright"
let at = NSMutableAttributedString(string: text)
let p1 = NSMutableParagraphStyle()
let p2 = NSMutableParagraphStyle()
p1.alignment = .left
p2.alignment = .right
p2.paragraphSpacingBefore = -label.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 4))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 4, length: 6))
label.numberOfLines = 0
label.attributedText = at

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;

It's possible to indirectly use NSTextTable which is unavailable on iOS.
If you create html table like:
<table width="100%">
<tr>
<td align="left">Left string</td>
<td align="right">Right string</td>
</tr>
</table>
And then convert it to an attributed string like that way:
extension NSAttributedString {
convenience init?(html: String) {
guard let data = html.data(using: .utf8) else {
return nil
}
try? self.init(data: data, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
], documentAttributes: nil)
}
}
You will get exactly what you need and if you check paragraph attributes in that attributed string - you will see NSTextTable there.

Related

iOS: how to change colour/font of some characters of a string

in my app i have to change the font of a part of string which comes from JSON response
> "<span class=\"drop_data_user\">Andrew James</span> liked your comment
> \"hiiiiiiiiiiiiiii\" that you posted."
to convert it in attributed string i am using the following code
NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[NotificationTxt dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:#(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
I want to change the font colour of the sender
Also, you can use it as follows.
NSString *dateString = [NSString stringWithFormat:#"%#%#", #"Teslimat: ", selectedReservationModel.DateLabel];
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString: dateString];
[attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansBoldFontSize:12] range:NSMakeRange(0, 8)];
[attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansRegularFontSize:12] range:NSMakeRange(9, selectedReservationModel.DateLabel.length)];
lblDeliveryDate.attributedText = attrS;
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"My String"];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 6)];
self.myLabel.attributedText = attrS;
For fonts use
[attrS addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 3)];
I believe the easiest way to fulfil your task is to use CSS inlined in the text, then parsing HTML as you do will change the color too (I'm using Swift syntax, but I believe you can easily convert it to ObjC):
let text = "<span class=\"drop_data_user\">Andrew James</span> liked your comment \"hiiiiiiiiiiiiiii\" that you posted."
let colouredAuthorText = "\(text)<style>.drop_data_user { color: #FEB600; }</style>"
Then just parse colouredAuthorText (which is created by appending style to the original text) instead of the original, and you should be good to go.

How to just add underline below the second line of text in a Bar Button Item?

This is what I want to achieve:
I'm thinking about having two separate attributed strings and combine them together. Not sure if this is the only way?
UPDATE
The button displays "(null)" if using setAttributedTitle. It can display the right string with no attributes if using setTitle.
Still cannot display in the intended way. Any idea?
// Set current bar button attributes
NSMutableAttributedString *currentBarAttributedString = [[NSMutableAttributedString alloc] init];
[currentBarAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:#"REQUEST\n"
attributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleNone)}]];
[currentBarAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:#"EQUIPMENT"
attributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)}]];
// Initialize buttons and set titles
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setAttributedTitle:currentBarAttributedString forState:UIControlStateNormal];
// [button1 setTitle:[currentBarAttributedString string] forState:UIControlStateNormal];
To add border to text or to change color.here is sample code which is used.
use This code in
- (void)viewDidLoad {
[super viewDidLoad];
NSString *strFirst = #"Request Equipment";
NSString *strSecond = #"Active Rentals";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:strFirst
attributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle),
NSForegroundColorAttributeName:[UIColor yellowColor]}]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:strSecond
attributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleNone),NSForegroundColorAttributeName:[UIColor whiteColor]}]];
//To use attribute string in button
[self.btnAttributeString setAttributedTitle:attributedString forState:UIControlStateNormal];
}
OutPut is
Please check this and let me know any issue.
Just create a NSAttributedString and format it as required
NSString *alertString = #"All heroes do not wear capes.";
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attrs = #{
NSParagraphStyleAttributeName: paragraphStyle,
//provide a nsdict here with attributes you want to apply to the whole of the string.
};
NSDictionary *subAttrs = #{
NSParagraphStyleAttributeName: paragraphStyle,
//Here provide attributes for the not underlined part of the string.
};
NSDictionary *subAttrs2 = #{
NSParagraphStyleAttributeName: paragraphStyle,
//Here provide attributes for the underlined part of the string
};
//Set the range of the sub attributes.
const NSRange range = NSMakeRange(0,3);
const NSRange range2 = NSMakeRange(5,4);
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:alertString
attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
[attributedText setAttributes:subAttrs2 range:range2];
Now set this attributed string as your attributed title
class UnderlinedLabel: UILabel {
override var text: String! {
didSet {
let textRange = NSMakeRange(0, count(text))
let textRange = NSMakeRange(0, text.characters.count)
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
}

Have multiple ranges with different attributes on NSAttributedString

I have a multiline UILabel of which I would like to increase the line height, but I also want part of it to be a different color, only the line height works fine. But as soon as I try to change the color for a certain range it just goes back to the stock appearance, no line either..
Anyone a tip? This is done in the content setter.
- (void)setContent:(NSString *)content {
_content = content;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.content];
NSMutableAttributedString *mutableAttrString = [attributedString mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing: 5.0f];
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont fontWithName:#"BentonSans-Regular" size:16.0],
NSParagraphStyleAttributeName: paragraphStyle
};
NSDictionary *colorAttributes = #{
NSForegroundColorAttributeName: [UIColor redColor]
};
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
[mutableAttrString addAttributes:colorAttributes range:NSMakeRange(4, 8)];
[self.label setAttributedText: mutableAttrString];
}
Thanks!
The NSRangeFromString function expects a string like #"{3,10}". In other words, it expects a string that contains two numbers that specify the starting location and length of the range. I suspect that the content string isn't a string like that.
So this line
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
should be
[mutableAttrString addAttributes:attributes range:NSMakeRange(0,mutableAttrString.length)];
in your viewdidLoad Method assign string to self.content :
self.content = #"pass your text ";
// Remove the First line of your method it is not needed
- (void)setContent:(NSString *)content {
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.content];
NSMutableAttributedString *mutableAttrString = [attributedString mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing: 5.0f];
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont fontWithName:#"BentonSans-Regular" size:16.0],
NSParagraphStyleAttributeName: paragraphStyle
};
NSDictionary *colorAttributes = #{
NSForegroundColorAttributeName: [UIColor redColor]
};
[mutableAttrString addAttributes:attributes range:NSRangeFromString(self.content)];
[mutableAttrString addAttributes:colorAttributes range:NSMakeRange(4, 8)];
[self.label setAttributedText: mutableAttrString];
}

Disabling UITextView Kerning

I'm trying to disable any kind of kerning in UITextView
Somehow in arabic letters the text view kern the letters to fit the line but it just ruin the whole word, here's an examples:
textview has white background, and the code is:
NSString *aya =[mainArray objectAtIndex:indexPath.row];
cell.tx.text = aya;
[cell.tx sizeToFit];
cell.tx.frame = CGRectMake(5, 5, 265, cell.tx.frame.size.height);
cell.tx.backgroundColor = [UIColor whiteColor];
also tried to set NSMutableAttributedString NSKernAttributeName value but didn't work,,
---------------------Edit: This is the code for Kern Attribute:
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:aya];
[attributedString addAttribute:NSKernAttributeName
value:#0.0
range:NSMakeRange(0,[aya length])];
[attributedString addAttribute:NSFontAttributeName
value:font
range:NSMakeRange(0,[aya length])];
NSMutableParagraphStyle *paragrapStyle = [NSMutableParagraphStyle new];
paragrapStyle.alignment = NSTextAlignmentRight;
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragrapStyle
range:NSMakeRange(0,[aya length])];
[cell.tx setAttributedText:attributedString];
It's a hack and I'm not near a computer to try it, but give this a shot:
Set the tx's text property
[tx setText:aya];
Create an NSMutableAttributedString from the text view's text
NSMutableAttributedString *attrStr = [[tx attributedText] mutableCopy];
Set the kern attribute on the attributed string
[attrStr setAttributes:#{ NSKernAttributeName: #(0.f) }
range:NSMakeRange(0, [attrStr length])];
Set the attrStr back on tx
[tx setAttributedText:attrStr];
This will (hopefully) preserve the attributes UITextView infers from your NSString.

NSAttributedString add text alignment

How can I add text alignment attribute to an NSAttributedString to center the text?
Edit:
Am I doing anything wrong? It doesn't seem to change the alignment.
CTParagraphStyleSetting setting;
setting.spec = kCTParagraphStyleSpecifierAlignment;
setting.valueSize = kCTCenterTextAlignment;
CTParagraphStyleSetting settings[1] = {
{kCTParagraphStyleSpecifierAlignment, sizeof(CGFloat), &setting},
};
CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, sizeof(setting));
NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedString];
[mutableAttributed addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:(NSObject*)paragraph ,(NSString*) kCTParagraphStyleAttributeName, nil] range:_selectedRange];
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSAttributedString *attributedString =
[NSAttributedString.alloc initWithString:#"someText"
attributes:
#{NSParagraphStyleAttributeName:paragraphStyle}];
Swift 4.2
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
let attributedString = NSAttributedString(string: "someText", attributes: [NSAttributedString.Key.paragraphStyle : paragraphStyle])
I was searching for the same issue and was able to center align the text in a NSAttributedString this way:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string];
[attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
Swift 4.0+
let titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.alignment = .center
let titleFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
let title = NSMutableAttributedString(string: "You Are Registered",
attributes: [.font: titleFont,
.foregroundColor: UIColor.red,
.paragraphStyle: titleParagraphStyle])
Swift 3.0+
let titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.alignment = .center
let titleFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
let title = NSMutableAttributedString(string: "You Are Registered",
attributes: [NSFontAttributeName:titleFont,
NSForegroundColorAttributeName:UIColor.red,
NSParagraphStyleAttributeName: titleParagraphStyle])
(original answer below)
Swift 2.0+
let titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.alignment = .Center
let titleFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
let title = NSMutableAttributedString(string: "You Are Registered",
attributes:[NSFontAttributeName:titleFont,
NSForegroundColorAttributeName:UIColor.redColor(),
NSParagraphStyleAttributeName: titleParagraphStyle])
As NSAttributedString is primarily used with Core Text on iOS, you have to use CTParagraphStyle instead of NSParagraphStyle. There is no mutable variant.
For example:
CTTextAlignment alignment = kCTCenterTextAlignment;
CTParagraphStyleSetting alignmentSetting;
alignmentSetting.spec = kCTParagraphStyleSpecifierAlignment;
alignmentSetting.valueSize = sizeof(CTTextAlignment);
alignmentSetting.value = &alignment;
CTParagraphStyleSetting settings[1] = {alignmentSetting};
size_t settingsCount = 1;
CTParagraphStyleRef paragraphRef = CTParagraphStyleCreate(settings, settingsCount);
NSDictionary *attributes = #{(__bridge id)kCTParagraphStyleAttributeName : (__bridge id)paragraphRef};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:#"Hello World" attributes:attributes];
Swift 4 answer:
// Define paragraph style - you got to pass it along to NSAttributedString constructor
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
// Define attributed string attributes
let attributes = [NSAttributedStringKey.paragraphStyle: paragraphStyle]
let attributedString = NSAttributedString(string:"Test", attributes: attributes)
In swift 4:
let paraStyle = NSMutableParagraphStyle.init()
paraStyle.alignment = .left
let str = "Test Message"
let attribute = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 12)]
let attrMessage = NSMutableAttributedString(string: str, attributes: attribute)
attrMessage.addAttribute(kCTParagraphStyleAttributeName as NSAttributedStringKey, value: paraStyle, range: NSMakeRange(0, str.count))
If you use UILabel, just use label.textAlignment = .center instead
Xamarin.iOS
NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.HyphenationFactor = 1.0f;
var hyphenAttribute = new UIStringAttributes();
hyphenAttribute.ParagraphStyle = paragraphStyle;
var attributedString = new NSAttributedString(str: name, attributes: hyphenAttribute);
[averagRatioArray addObject:[NSString stringWithFormat:#"When you respond Yes to %# the average response to %# was %0.02f",QString1,QString2,M1]];
[averagRatioArray addObject:[NSString stringWithFormat:#"When you respond No to %# the average response to %# was %0.02f",QString1,QString2,M0]];
UIFont *font2 = [UIFont fontWithName:#"Helvetica-Bold" size:15];
UIFont *font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"When you respond Yes to %# the average response to %# was",QString1,QString2]];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,[#"When you respond Yes to " length])];
[str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange([#"When you respond Yes to " length],[QString1 length])];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange([QString1 length],[#" the average response to " length])];
[str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange([#" the average response to " length],[QString2 length])];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange([QString2 length] ,[#" was" length])];
// [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(49+[QString1 length]+[QString2 length] ,8)];
[averagRatioArray addObject:[NSString stringWithFormat:#"%#",str]];

Resources