Prevent line break in a NSAttributedString - ios

I think it's a common issue when you have a set of words where you don't want a break line.
Sometimes the character between those words is a space or a hyphen, etc. In my case it's a point :)
This is my text 50.0/80.0
At the end I did it using the size label and measuring how much space I need for that string in particular:
UIFont *fontAwardNumber = [UIFont fontWithName:#"DIN-Bold" size:20];
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize labelSize = (CGSize){customCell.awardValueLabel.bounds.size.width, FLT_MAX};
CGRect rectNeededForAwardNumber = [awardNumber boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: fontAwardNumber} context:context];
if (rectNeededForAwardNumber.size.height > customCell.awardValueLabel.bounds.size.height) {
//We need to add a breakline
NSRange range = [awardNumber rangeOfString:#"/"];
if (range.location != NSNotFound) {
awardNumber = [awardNumber stringByReplacingCharactersInRange:range withString:#"/\n"];
}
}
I found other solutions like replacing your space or hyphen for unbreakable characters:
Preventing line breaks in part of an NSAttributedString
But my question is more general, does NSAttributedString provide something to define a set of words as non breakable? Or is there any easier way to do it for a general set of words?

No, NSAttributedString doesn't have any per-character attributes that preventing line breaking within a range. You can set the NSLineBreakMode to ByClipping or another non-wrapping mode in the NSParagraphStyle, but that applies to all the text in the paragraph. (Paragraphs are separated by newlines.)
To prevent line breaking in a smaller range than a whole paragraph, you need to insert a U+2060 WORD JOINER between any two characters where an unwanted break might occur. In your example, that means on each side of the slash character.

Related

Xcode UILabel bug? Line spacing cropping text with a UILabel

I have some designs I'm following for an iOS project. The font used is Avenir with relatively tight line spacing.
Some of these labels will have dynamic text, so I can't just make the label's size larger since the size should be determined by the content.
By default line spacing for a UILabel ends up pretty large.
If I adjust the Line Height Multiple or the Max Height, the text along the top ends up cropped.
It should behave like this (Affinity Designer)...
Is there a way to handle this?
Thanks for your help!
This works for me. By adding
minimumLineHeight
let string = NSMutableAttributedString(string: venue.name)
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 0.68
style.minimumLineHeight = nameLabel.font.lineHeight
string.addAttribute(NSAttributedString.Key.paragraphStyle,
value: style,
range: NSMakeRange(0, venue.name.count))
nameLabel.attributedText = string
Unfortunately the UILabel has several quirks when it comes to vertical adjustments. A somewhat hacky solution is to move the baseline of the first line down as needed. Depending on if your string ends with a newline, and the amount of tightening you do, you might need to add one or two extra newlines also, otherwise the rendering engine will clip the last line.
The code snippet assumes that self.label already has an attributed string assigned to it, and that it has line separator character 0x2028 between the lines. This is usually true when entering multi-line text in IB.
// 0x2028 is the unicode line separator character
// Use \n instead if it is what you have
// or calculate the length of the first line in some other way
NSInteger lengthOfFirstLine = [self.label.text componentsSeparatedByString:#"\u2028"][0].length;
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithAttributedString:self.label.attributedText];
// Add two more blank lines so that the rendering engine doesn't clip the last line
[s appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n\n"]];
// Move the baseline offset for the first line down
// the other lines will adjust to this
// 50 is a value you will have to find what looks best for you
[s addAttribute:NSBaselineOffsetAttributeName value:#(-50) range:NSMakeRange(0, lengthOfFirstLine)];
self.label.attributedText = s;

UILabel adjustFont with multiline

I have a UILabel which usually has to display one or two words.
Many times one of the words doesn't fit into one line, so I would like to reduce font size in order to fit each word at least in one line (not breaking by character).
Using the technique described in http://beckyhansmeyer.com/2015/04/09/autoshrinking-text-in-a-multiline-uilabel/
self.numberOfLines = 2;
self.lineBreakMode = NSLineBreakByTruncatingTail;
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.65;
I've found that it plays well when the second word doesn't fit in just one line.
But it doesn't when there is just one word, or the first word is the one
that doesn't fit.
I managed to solve the case of just one word doing this:
-(void)setText:(NSString *)text
{
self.numberOfLines = [text componentsSeparatedByString:#" "].count > 1 ? 2 : 1;
[super setText:text];
}
But how could I solve those cases where the first word doesn't fit?? Any ideas?
How about this ?
self.numberOfLines = [text componentsSeparatedByString:#" "].count;
[self setAdjustsFontSizeToFitWidth:YES];
But, this will rule out the case where your label text consists of two very small words, eg."how are". In such cases, the entire string will be visible in the first line itself. If it is your requirement to display each word in a separate line then i would recommend you adding a '\n' after every word. This means that you will have to edit the string before assigning it to the label. Thus, a universal solution could be like :
NSString *string = #"how are"; //Let this be the string
NSString *modifiedString = [string stringByReplacingOccurrencesOfString:#" " withString:#"\n"];
[self setText:modifiedString];
[self setTextAlignment:NSTextAlignmentCenter];
[self setAdjustsFontSizeToFitWidth:YES];
[self setNumberOfLines:0];
self.lineBreakMode = NSLineBreakByWordWrapping;

How to put different headIndent (alignment) for every paragraph in UITextView

In my application i need to align all the paragraph differently.
like, first paragraph's headIndent is 0.0f then second's 10.0f and third's 3.0f.
i am giving all the paragraph style to textview.attributedText. and it took only one style.
Here whole text will come dynamically by Typing. means when User will type in text view at that time. so, there are no static string to do this.
I am placing all the characters in UITextView by this...
UIFont *fontBold = [UIFont fontWithName:#"Helvetica-Bold" size:15];
attributesHelveticaBold = #{NSFontAttributeName :fontBold};
UIFont *fontNormal = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
attributesNormal = #{NSFontAttributeName :fontNormal};
if (varBold== 1) {
[textView setTypingAttributes:attributesHelveticaBold];
}
else {
[textView setTypingAttributes:attributesNormal];
}
And i want to get this kind of result in text view
When i am typing the typing become slow too.
but i think i'll come over that issue but for now i stuck on this alignment problem.
how to do it when bullet point come and when different text come.
any kind of link, code, tutorial will be great help...
---------- Edit : ----------
Please have a look in Evernote's application.
I need to do the exactly same thing in my app. for alignment of second,third,etc line when bullet come.
-------- Edit after searching :-------
I searched too much for this but ain't find anything by googling.
So, now i am asking if anyone now about How to give any paragraph style or any attribute style to a paragraph and just leave it as it is on text view and then perform other paragraph style on second paragraph. at this time the first paragraph will not pass throw "shouldChangeTextInRange" method.
yes, it's quite confusing whatever i am saying.
so i explaining it in general...
if user set the text view's first paragraph's headIndent=7.0f then when user will type next paragraph and set the headIndent = 13.0f then first paragraph will stay as it is in textview and just running paragraph will come in a chapter (means in a method).
right now i am doing these thing in shouldChangeTextInRange method to do style for each paragraph.
varStaringPointOfString = 0;
varEndingPointOfString = 0;
NSArray *sampleArrToGetattrStr = [txtViewOfNotes.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (int i=0; i<[sampleArrToGetattrStr count]; i++)
{
NSString *strtostoreLength = [NSString stringWithFormat:#"%#",[sampleArrToGetattrStr objectAtIndex:i]];
varStaringPointOfString = (int)strtostoreLength.length + varEndingPointOfString;
if ([strtostoreLength hasPrefix:#"\t•\t"])
{
[[textView textStorage] addAttribute:NSParagraphStyleAttributeName value:paragraphStyleForBullet range:NSMakeRange(varEndingPointOfString, strtostoreLength.length)];
}
else
{
[[textView textStorage] addAttribute:NSParagraphStyleAttributeName value:paragraphStyleNormal range:NSMakeRange(varEndingPointOfString, strtostoreLength.length)];
}
varEndingPointOfString = varStaringPointOfString;
strtostoreLength =#"";
}
but from this the speed of typing is become very slow.
Try this:
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstHeadLineHeadIndent:firstLineIndend]; //Only the first line
[paragraphStyle setHeadIndent:headIndent]; //The rest of the lines, except the first one
[yourAttributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:paragraphRange];
For the bullet point, that's something different. You need to find where are the bullet point, and set another indent accordingly.

iOS: sizeWithFont for single character is different than in a string?

I am trying to determine the precise position of a character in a UILabel, say:
(UILabel *)label.text = #"Hello!";
I'd like to determine the position of the 'o'. I thought that I could just sum the widths of all the preceding characters (or the whole preceding string) using sizeWithFont. The width value I get though is bigger by about 10% than what it should be. Summing the widths of individual letters (i.e. [#"H" sizeWithFont...] + [#"e" sizeWithFont...] + l... + l...) accumulates more error than [#"Hell" sizeWithFont...].
Is there a way of accurately determining the position of a single glyph in a string?
Many thanks.
Yes, but not in a UILabel and not using sizeWithFont:.
I recently worked with Apple Developer Support, and apparently sizeWithFont: is actually an approximation. It becomes less accurate when your text (1) wraps across multiple lines and (2) contains non-latin characters (i.e. Chinese, Arabic), both of which cause line spacing changes not captured by sizeWithFont:. So, don't rely on this method if you want 100% accuracy.
Here are two things you can do:
(1) Instead of UILabel, use a non-editable UITextView. This will support the UITextInput protocol method firstRectForRange:, which you can use to get the rect of the character you need. You could use a method like this one:
- (CGRect)rectOfCharacterAtIndex:(NSUInteger)characterIndex inTextView:(UITextView *)textView
{
// set the beginning position to the index of the character
UITextPosition *beginningPosition = [textView positionFromPosition:textView.beginningOfDocument offset:characterIndex];
// set the end position to the index of the character plus 1
UITextPosition *endPosition = [textView positionFromPosition:beginningPosition offset:1];
// get the text range between these two positions
UITextRange *characterTextRange = [textView textRangeFromPosition:beginningPosition toPosition:endPosition]];
// get the rect of the character
CGRect rectOfCharacter = [textView firstRectForRange:characterTextRange];
// return the rect, converted from the text input view (unless you want it to be relative the text input view)
return [textView convertRect:rectOfCharacter fromView:textView.textInputView];
}
To use it, (assuming you have a UITextView called myTextView already on the screen), you would do this:
myTextView.text = #"Hello!";
CGRect rectOfOCharacter = [self rectOfCharacterAtIndex:4 inTextView:myTextView];
// do whatever you need with rectOfOCharacter
Only use this method for determining the rect for ONE character. The reason for this is that in the event of a line break, firstRectForRange: only returns the rect on the first line, before the break.
Also, consider adding the method above as a UITextView category if you're gong to be using it a lot. Don't forget to add error handling!
You can learn more about how firstRectForRange: works "under the hood" by reading the Text, Web, and Editing Programming Guide for iOS.
(2) Create your own UILabel by subclassing UIView and using Core Text to render the strings. Since you're doing the rendering, you'll be able to get the positions of characters. This approach is a lot of work, and only worthwhile if you really need it (I, of course, don't know the other needs of your app). If you aren't sure how this would work, I suggest using the first approach.
Well fonts are smart now a day and take in respect the position of a character to its pervious character.
Here is an example on how the starting position of the letter o:
NSRange posRange = [hello rangeOfString:#"o"];
NSString *substring = [hello substringToIndex:posRange.location];
CGSize size = [substring sizeWithFont:[UIFont systemFontOfSize:14.0f]];
No you can do the same for the string including the letter o and substract the size found in the string without the letter o.
THis should give the an nice start position of the letter and the size.
in ios6 you can do using attributed string
NSMutableAttributedString *titleText2 = [[NSMutableAttributedString alloc] initWithString:strHello];
NSRange posRange = [hello rangeOfString:#"o"];
[titleText2 addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName] range:NameRange];
and set your textView with this attributed string

How to truncate an NSString based on the graphical width?

In UILabel there's functionality to truncate labels using different truncation techniques (UILineBreakMode). In NSString UIKit Additions there is a similar functionality for drawing strings.
However, I found no way to access the actual truncated string. Is there any other way to get a truncated string based on the (graphical) width for a given font?
I'd like to have a category on NSString with this method:
-(NSString*)stringByTruncatingStringWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
- (NSString*)stringByTruncatingStringWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode {
NSMutableString *resultString = [[self mutableCopy] autorelease];
NSRange range = {resultString.length-1, 1};
while ([resultString boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size.width > width) {
// delete the last character
[resultString deleteCharactersInRange:range];
range.location--;
// replace the last but one character with an ellipsis
[resultString replaceCharactersInRange:range withString:truncateReplacementString];
}
return resultString;
}
Note that since iOS 6 this method is not safe to run on background threads anymore.
One option is trying different sizes by looping until you get the right width. I.e. start with the full string, if that's wider than what you need, replace the last two characters with an ellipsis character. Loop until it's narrow enough.
If you think you'll be working with long strings, you can binary search your way towards the truncation point to make it a bit faster.

Resources