How to adjust NSLineBreakByTruncatingTail to a portion of NSAttributedString? - ios

I want to adjust NSLineBreakByTruncatingTail to a portion of NSAttributedString.
For example,
The result is below.
"abcde...LAST"
The 'abcde...' is a NSAttributedString with the properties different with that of 'LAST'.
Actually, I can do if 'abcde...' string object and 'LAST' string object are diffrent.
But, I want to make to same string object.
sample code is below.
NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
[style2 setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:#"abcdefghijklm"];
[attrString1 addAttribute:NSForegroundColorAttributeName:[UIColor whiteColor]];
NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] initWithString:#"LAST"];
[attrString2 addAttribute:NSForegroundColorAttributeName:[UIColor blueColor]];
[attrString1 appendAttributedString:attrString2];
Then, I expected like the third line.
But the result is like this.
"abcdefghij..."

Related

How to arrange NSString word in this type format without adding space in UILabel

I want to make this type format to word arrange in NSString on UILabel like
but I try to make add extra white space according to upper line word length but another way to make this type format suppose using NSAttributesString.
You can do this using NSMutableAttributedString without adding extra white space.
First, create a method that returns NSMutableAttributedString like this-
-(NSMutableAttributedString*)setIndent:(NSString*) title value:(CGFloat) value {
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentLeft;
style.firstLineHeadIndent = value;
NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:title attributes:#{ NSParagraphStyleAttributeName : style}];
return attrText;
}
and use this method by the following way -
NSString *title = #"Charlie Chapline Cartoon";
NSArray* foo = [title componentsSeparatedByString: #" "];
NSMutableAttributedString *attrText5 = [[NSMutableAttributedString alloc] init];
CGFloat value = 0.0f;
for(int i = 0; i< foo.count; i++){
//change this value according to your need.
value = value + 20.0f;
[attrText5 appendAttributedString:[self setIndent:[NSString stringWithFormat:#"%#\n", foo[i]] value:value]];
}
_myLab.numberOfLines = 5;
_myLab.attributedText = attrText5;
Output:

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
}
}
}

Multiple text alignment with same label using NSMutableAttributedString on iOS

I want to set Text direction for my label using NSMutableAttributedString. for example, I have chat view, which contains message and time. I want to set left alignment for message and right alignment for time using UILabel.
I have used following code, but it's not working,
NSString *Time = [Functions stringFromGivenDate:msg.time withFormate:#"hh:mm a"];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",msg.text,Time]];
NSDictionary *attrDictionary = #{NSWritingDirectionAttributeName:#[#(NSTextWritingDirectionOverride)]};
[str addAttributes:attrDictionary range:NSMakeRange(msg.text.length+1, Time.length)];
if I understood correctly, this should help:
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = alignment;// type NSTextAlignment
NSDictionary *attributtes = #{NSParagraphStyleAttributeName : style,};

Justified text with UITextView and NSMutableAttributedString

I'm trying to put a justified text for a UITextView with NSMutableAttributedString, the NSMutableAttributedString is composed of different NSAttributedString because I need bold and regular font, so I append different NSString, this is my NSMutableAttributedString:
NSAttributedString *one = [[NSAttributedString alloc] initWithString:#"abc"
attributes:boldDict];
NSAttributedString *two = [[NSAttributedString alloc] initWithString:#" def"
attributes:regularDict];
NSAttributedString *three = [[NSAttributedString alloc] initWithString:#" ghi"
attributes:boldDict];
NSMutableAttributedString *string =
[[NSMutableAttributedString alloc] initWithAttributedString:one];
[string appendAttributedString:two];
[string appendAttributedString:three];
I have tried this:
[self.text_view setTextAlignment:NSTextAlignmentJustified]
and this:
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;
Dictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
and apply this to NSMutableAttributedString, but neither works. how i can do?
I fixed the same problem by specifying transparent background color for the NSAttributedString.
Looks like a bug in the UILabel code which should render simple NSAttributedString like NSString.
Xamarin.iOS example:
var paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.Alignment = UITextAlignment.Justified;
var attributedText = new NSAttributedString(simpleString,
paragraphStyle: paragraphStyle,
backgroundColor: Color.Transparent.ToUIColor());
myLabel.AttributedText = attributedText;

Nsattributedstring with different NSMutableParagraphStyle

I have 1 attributed string with multiple paragraph.
I had given the FirstLineHeadIndent = 2.12.
Now I want to give the FirstLineHeadIndent=0 to 1st paragraph
and FirstLineHeadIndent=2 to 2nd paragraph in the attributed string.
If I set the property like
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing =Line_space;
paragraphStyle.firstLineHeadIndent =2;
paragraphStyle.headIndent =margin_space;
paragraphStyle.tailIndent =-margin_space;
paragraphStyle.paragraphSpacing=paragraph_space;
NSDictionary *ats = #{
NSFontAttributeName : [UIFont fontWithName:self.bookView.defaultFontFamily size:self.bookView.defaultFontSize],
NSParagraphStyleAttributeName : paragraphStyle,
};
It will give the head space to both the paragaraph.
Please help me.
I am uploading the image for more help:-
So, the solution is to use 2 paragraph styles.
Your NSString is separated by a \n, that indicate the separations between the 2 paragraphs.
NSMutableParagraphStyle *firstParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[firstParagraphStyle setFirstLineHeadIndent:0];
//Do the rest of the settings
NSMutableParagraphStyle *secondParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[secondParagraphStyle setFirstLineHeadIndent:2];
//Do the rest of the settings
//You may use the same paragraphStyle, changing just the firstLineHeadIndent, and set the attributes, but for a clearer explanation, I used 2 paragraph styles
NSRange range = [[yourAttributedString string] rangeOfString:#"\n"];
[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: firstParagraphStyle range:NSMakeRange(0, range.location)]; //The range is from the start to the \n
[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: secondParagraphStyle range:NSMakeRange(range.location, [[yourAttributedString string] length]-range.location)]; //The range is from the start of \n to the end

Resources