NSString sizing does not account for Greek breathing marks - ios

I've got an app that displays Greek text. I use the Cardo font for good display. In working on an AppleWatch extension and app, it was pointed out to me that some of the special characters are being cut off. This is how some example text should look (screenshot from an iPhone simulator):
Here is the same text on the Watch simulator:
Note that the fancy accent character (to be specific, a breathing mark with a circumflex accent) on the second character of the first word is cut off. I tried setting the label's frame on the phone using some NSString measuring code like this:
UILabel *label = [[UILabel alloc]init];
label.font = [UIFont fontWithName:#"Cardo" size:16];
[self.view addSubview:label];
label.text = #"οὗτος ἦλθεν εἰς μαρτυρίαν ἵνα μαρτυρήσῃ περὶ τοῦ φωτός, ἵνα πάντες πιστεύσωσιν δι᾽ αὐτοῦ.";
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.lineBreakMode = NSLineBreakByWordWrapping;
CGRect rect = [label.text boundingRectWithSize:self.view.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName: label.font,
NSParagraphStyleAttributeName: style} context:nil];
label.frame = CGRectMake(5, 100, ceilf(rect.size.width), ceilf(rect.size.height));
label.layer.borderWidth = 1; //for clarity
label.layer.borderColor = [UIColor blackColor].CGColor;
The result looks like this (the border is drawn for clarity's sake):
Interestingly, if I use the system font instead of Cardo, the extra symbols display correctly:
So, my question: What causes the NSString sizing to cut off the extra marks? Is there some option I can pass to the sizing method to correct this? Or better yet, is there some option I can set on the WKInterfaceLabel in the Watch app to get it to render correctly?

I don't know if this will fix it or not, but this will give you more to look into. I ran into an issue with a font using "Lower Case" numbers, which is a typography style for tabular numbers. I was able to create a variation of the font using a UIFontDescription that forced all numbers to be upper case. In your case the font's ascenders go over the top of the font's capHeight. There are a bunch of options for creating a font using a font descriptor and one of them may help. Here is how I created the upper case number font.
_font = [UIFont fontWithName:_fontName size:size];
NSArray* featureSettings = #[
#{
UIFontFeatureTypeIdentifierKey: #(kNumberCaseType),
UIFontFeatureSelectorIdentifierKey: #(kUpperCaseNumbersSelector)
}];
UIFontDescriptor* originalDescriptor = [_font fontDescriptor];
UIFontDescriptor* newDescriptor = [originalDescriptor fontDescriptorByAddingAttributes: #{UIFontDescriptorFeatureSettingsAttribute: featureSettings }];
_font = [UIFont fontWithDescriptor: newDescriptor size: size];
Specifically check out the kVerticalPositionType.
I actually ran into a case in my iOS app where the font ascenders where going over the capHeight from the baseline up and my attributed string was getting cut off. Since I was in quartz drawing code I just use the difference of the font's lineHeight with its ascender and padded the top. Unfortunately that is not an option on the Apple Watch.

I suspect the problem is in the font itself, with the ascenders being set too tightly for proper display.
I would first try setting the UILabel's text inset by making a subclass of UILabel and overriding drawTextInRect:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {5, 0, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
If you have tried increasing the text inset to accommodate the font and it has not worked, take a look at Custom installed font not displayed correctly in UILabel

Related

How can I make a UITextView layout text the same as a UILabel?

I have a UILabel that I need to convert to a UITextView because reasons. When I do this, the text is not positioned the same, despite using the same (custom) font.
I found that if I set:
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
This gets the text very close, but if I superimpose the UITextView over top of the UILabel, I see the text positioning get farther apart with each new line.
The UILabel is green, the UITextView is black. This is using NSParagraphStyle to set min and max line height to 15.
I've played with setting the paragraph style and min/max line height, but I haven't been able to match it exactly. I'm not a printer, so I don't necessarily understand all of the font related terms in the documentation for NSLayoutManager and NSTextContainer and all that.
I only need to support iOS 7 and up.
I'm not going to switch to some crazy CoreText-based custom widget or use some random third party library. I'm okay with close enough if I have to. But it seems like there should be some combination of random properties to make them layout the same.
I took the solution for line spacing found at this link and applied it to your issue. I managed to get it incredibly close by adjusting the lineSpacing property. I tested with HelveticaNeue size 13 and managed to get it to line up as shown in the screen shot below.
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -0.38;
NSDictionary *attrsDictionary =
#{ NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:13.0f],
NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attrsDictionary];
I've been able to successfully 'impersonate' a non-editable multiline UILabel (as it happens, in a UITableViewCell subclass) with an equivalent editable multiline UITextView using the following :
_textView = UITextView.new;
_textView.font = _label.font;
_textView.textColor = _label.textColor;
_textView.textAlignment = _label.textAlignment;
_textView.backgroundColor = UIColor.clearColor;
_textView.textContainer.lineFragmentPadding = 0;
_textView.textContainerInset = UIEdgeInsetsZero;
and to make it behave well when doing actual edits, add the following to your UITextViewDelegate:
- (void)textViewDidChange:(UITextView *)textView
{
...
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
[textView scrollRectToVisible:[textView caretRectForPosition:textView.endOfDocument] animated:NO];
}

How to use NSLineBreakByCharWrapping with setNumberOfLines:1 on iOS?

For example, if the text for a UILabel is: "Questions that may already have your answer", I want it to print
"Questions that may alr". I don't want to clip the last character but cut the text at that point.
I cannot use a character limit as most fonts are not monospaced. I cannot use clipping as It may cut the text at another point or it may cut that last letter "r" from any where of it.
The behaviour I want is similar to NSLineBreakByCharWrapping while numberOfLines = 0.
So, I want it to drop(wrap) that non-fitting last character but I want it to drop/wrap to a hidden space = I don't want that second line.
How can this be possible?
To get required output you need to know width of the label, which can be known by following method:
CGSize size = [string sizeWithAttributes:
#{NSFontAttributeName:
[UIFont systemFontOfSize:17.0f]}]; // iOS 7 and later
CGSize size = [string sizeWithFont: [UIFont systemFontOfSize:17.0f]]; // prior to iOS 7
set the size of your label with the above calculated size.
set the numberOfLines property to 1;
set the lineBreakMode property to NSLineBreakByCharWrapping;
UILabel *yourLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, size.width, 50)];
placeLbl.numberOfLines = 1;
placeLbl.lineBreakMode = NSLineBreakByCharWrapping;
In swift 3.0
labelObject.numberOfLines = 0;
labelObject.lineBreakMode = NSLineBreakMode.byCharWrapping

UILabel not drawing multiline

I want the label below (in yellow) to be at least two lines rather one.
I've made sure to uncheck Use Autolayout in Interface Builder. When I set the numberOfLines from 0 to 2, I get two words stacked on top of each other, with the yellow background tightly fitting the words. The result is the same regardless of whether the lineBreakMode is NSLineBreakByWordWrapping or NSLineBreakByTruncatingTail. It's also the same if I set the frame of the terms Label using the result of sizeWithAttributes or not, and it's the same if I use sizeToFit or not. I've also tried making the label a UILabel rather than a subclass of UILabel, which is TTTAttributedLabel, but the result is the same.
_termsLabel.font = [UIFont systemFontOfSize:12];
_termsLabel.textColor = [UIColor grayColor];
_termsLabel.textAlignment = NSTextAlignmentCenter;
_termsLabel.lineBreakMode = NSLineBreakByWordWrapping;
_termsLabel.numberOfLines = 0;
_termsLabel.delegate = self;
_termsLabel.backgroundColor = [UIColor yellowColor];
// Terms label
NSString *termsText = [NSString stringWithFormat:#"%# %# %# %#", NSLocalizedString(#"TermsIAgree", nil),
NSLocalizedString(#"SettingsTOS", nil),
NSLocalizedString(#"LocalizedAnd", nil),
NSLocalizedString(#"SettingsPrivacyPolicy", nil)];
_termsLabel.text = termsText;
_termsLabel.linkAttributes = #{ (__bridge NSString *)kCTUnderlineStyleAttributeName : [NSNumber numberWithBool:YES]};
CGSize termsSize = [_termsLabel.text sizeWithAttributes: #{ NSFontAttributeName : _termsLabel.font}];
_termsLabel.frame = CGRectMake(65,
395,
termsSize.width, termsSize.height);
[_termsLabel addLinkToURL:[NSURL URLWithString:TOS_URL] withRange:[termsText rangeOfString:NSLocalizedString(#"SettingsTOS", nil)]];
[_termsLabel addLinkToURL:[NSURL URLWithString:PRIVACY_POLICY_URL] withRange:[termsText rangeOfString:NSLocalizedString(#"SettingsPrivacyPolicy", nil)]];
EDIT: By finding the terms text size using CGSize termsSize = [_termsLabel.text sizeWithFont:_termsLabel.font forWidth:200 lineBreakMode:NSLineBreakByWordWrapping];
Yet the height of the termsSize is then 14, resulting in just one line:
How can I get the second line? SOLUTION At this point, just add [_termsLabel sizeToFit].
If you've got static text, just set the break mode to wrap, set lines to the number you want, and adjust the label's frame in interface builder until it wraps the way you like. Of you've got dynamic text, you can use sizeToFit after setting the label's text to have it automatically adjust it's height to fit the specified width:
Set frame to max desired width
Set lines to 0
Set break mode to wrap
Call sizeToFit
Determine the maximum width of your label and try sizeWithFont:forWidth:lineBreakMode: method with this value and desired NSLineBreakMode to get the size of resulting string's bounding box.

NSAttributeString Wrapping issue

I am trying to set attribute text to a label. The attributes seems to be a working the font as well as the color.
Only issue I am facing is the wrapping of lines. The Size of the UILabel is (200,300) with numberofLines=0. So with this it should wrap the lines, but it is not happening so.
NSMutableString *title=[[NSMutableString alloc] init];
NSRange range1;
NSRange range2;
NSRange range3;
NSString *str1=#"ABCD EFGHI klm";
[title appendString:str1];
range1=NSMakeRange(0, str1.length);
NSString *str2=#"PQRSSSS ";
[title appendString:str2];
range2=NSMakeRange(range1.length, str2.length);
NSString *str3=#"1235 2347 989034 023490234 90";
[title appendString:str3];
range3=NSMakeRange(range2.location+range2.length, str3.length);
NSMutableAttributedString *attributeText=[[NSMutableAttributedString alloc] initWithString:title];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color1,NSForegroundColorAttributeName,[self getStlylishItalicFont:13.0] ,NSFontAttributeName,nil] range:range1];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color2,NSForegroundColorAttributeName,[self getStylishFont:13.0] ,NSFontAttributeName,nil] range:range2];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color3,NSForegroundColorAttributeName,[self getStylishBoldFont:13.0] ,NSFontAttributeName,nil] range:range3];
self.myTextLabel.attributedText=attributeText;
UILabel is displayed like this, even though the height is 300.
ABCD EFGHI klm PQRSSSS 1235 234 ...
What you need is the NSParagraphStyle attribute :
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 1;
//Now add this to your attributes dictionary for the key NSParagraphStyleAttributeName eg,
#{NSParagraphStyleAttributeName:paragraphStyle,...}
On an unrelated note you know its better to create dictionaries in the modern Objective-c format. Whenever I don't, my mentor get's angry. That would look something like this :
[attributeText setAttributes:#{NSForegroundColorAttributeName:color1, NSFontAttributeName:[self getStlylishItalicFont:13.0], NSParagraphStyleAttributeName:paragraphStyle, }];
//The trailing comma in the dictionary definition is not at typo it is important.
Make sure you set your UILabel's line break mode attribute to the one you desired like so:
UILabel.lineBreakMode = NSLineBreakByWordWrapping;
Or if you are using Interface Builder, you can do it there.
Why can't you set the numberoflines to 1.Because wrapping makes sense but number of lines 0 doesnt make sense.. and also you have to set proper frame for label.
I think so u are facing the problem with the uilabel increasing the height , If u need the multiple lines in label then u have to give the property of havin numberoflines=0; after that u have to resize the label frame according to the size of text u are giving to the label.
Please check the below code i may be useful to u,
NSString *someText = [NSString stringWithFormat:#"%#",[[arrFulldetails objectAtIndex:indexPath.row]valueForKey:#"MessageText"]];
CGSize constraintSize;
constraintSize.width = 300.0f;
constraintSize.height =100000;
CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
CGRect rect ;
rect = CGRectMake(10, 150, 210, 20+stringSize.height);
along with setting:
self.myTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myTextLabel.numberOfLines = 0;
You might also need to set your label's layout constraints to it's superview. I ran into this when I neglected to pin my label's trailing constraint to the my label's superview.

iOS: sizeWithFont: for custom font

I've been plugging away and I need to use the sizeWithFont: method to properly line up my layout, but it doesn't seem to work with custom fonts. Is there a way to get it to work, or maybe another method I can use?
I'm pretty stumped on this. Any help is appreciated.
sizeWithFont should definitely work, but if it's giving you a problem then there is a workaround. You can put the text in a UITextView, add it to your layout, and then retrieve the actual text size and adjust accordingly. Here's some sample code from one of my projects:
// Put in the title in
self.titleView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 195, 195)];
self.titleView.font = [UIFont fontWithName:#"Bliss Pro" size:20.0];
[self addSubview:self.titleView];
self.titleView.text = #"Add your text here";
// Now get the actual size of the text and resize the title view's frame
CGRect frame = self.titleView.frame;
frame.size.height = self.titleView.contentSize.height;
self.titleView.frame = frame;
It's a bit hackish, but it certainly works.
#define kFontSize 14.0 //modify your font size
CGSize size = [yourView sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize]];

Resources