How to Fix text of UILabel word Character cut on Bottom side [duplicate] - uilabel

This question already has answers here:
UILabel cuts off custom font. How do I dynamically adjust UILabel height based on custom font selected?
(3 answers)
Closed 6 years ago.
my following Code is
NSMutableParagraphStyle *body1stParagraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
body1stParagraph.alignment = NSTextAlignmentCenter;
body1stParagraph.minimumLineHeight = _font_Size;
attrs = #{ NSFontAttributeName : [UIFont fontWithName:font_name size:Font_size],
NSParagraphStyleAttributeName : body1stParagraph,
NSAttachmentAttributeName: textAttachment};
and this Dictionary add on Attributed String
[attributeString addAttributes:attrs range:lineRange];
but text of label is Cut

this may solve your problem -
yourLabel.sizeToFit()

Related

set color of any UI in RGB format [duplicate]

This question already has answers here:
Making RGB color in Xcode
(5 answers)
Closed 5 years ago.
When I am setting color of any UI like text field place holder color or Slider maximum and minimum tint color in RGB format, It is not showing any color but just like a clear color. When I am changing red color of RGB to 0 it is showing mix color of green and blue. But as I increases value of Red in RGB up to 1 it is going to be faint and at 1, it is invisible. my code is
To set slider color
[_sliderSpeed setMaximumTrackTintColor:[UIColor colorWithRed:181 green:217 blue:255 alpha:1]];
To set text field placeholder color
NSAttributedString *pin = [[NSAttributedString alloc] initWithString:#"Add pin" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:0 green:103 blue:169 alpha:1] }];
self.pin.attributedPlaceholder = pin;
Please help me.
It should be like this
[_sliderSpeed setMaximumTrackTintColor:[UIColor colorWithRed:(181/255.0) green:(217/255.0) blue:(255/255.0) alpha:1] ;//set your color here
For Ref:
https://stackoverflow.com/a/10379026

Providing *hint* in text field in ios 7 as edittext field in android

I am creating a feedback form in an ios7 application and want to place few UITextFields which contain hints in the respective UITextField for name, phone number, email address, etc. I am using xcode 5 and ios7 for programming and creating application without use of storyboard.
The hint property is available in android for edit text field, but I am unable to find any such property in UITextField in ios 7.
Make use of the Placeholder text property.
Here's an example:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 50)];
textField.font = [UIFont fontWithName:#"Arial" size:20];
textField.placeholder = #"Your hint here.";
If you are using a storyboard or XIB, this property can be edited there also.
Also, if you want to use custom attributes for the placeholder, you can use the attributedPlaceholder property:
NSAttributedString *attribString = [[NSAttributedString alloc] initWithText:#"Your hint here." attributes:#{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:16.0]}];
textField.attributedPlaceholder = attribString;

NSTextView with shadow

I'm trying to add a nice looking shadow to a NSTextViews string, I have this Code so far:
NSShadow *textShadow = [[NSShadow alloc] init];
textShadow.shadowColor = [[NSColor blackColor]
colorWithAlphaComponent:0.3];
textShadow.shadowOffset = NSMakeSize(5.0, -5.0);
textShadow.shadowBlurRadius = 3;
NSDictionary *d = #{NSShadowAttributeName : textShadow,
NSFontAttributeName : [NSFont fontWithName:#"Arial Black" size:36.0],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSStrokeColorAttributeName : [NSColor whiteColor]};
[tv setTypingAttributes:d];
all in all this brings up a pretty looking Drop shadow on the right and the bottom of the string in the NSTextView but because the internal drawing mechanism of the textview seems to draw the "fill" of the Characters first and then the stroke around it, the Shadow lays above the fill of the text in the upper left of the Chars, which looks very bad as you can see here(would post an Image but not enough reputation right now 8-/ )
Is there a better way to add the shadow or a way to "raise" the fill color of the String so it lays above the shadow or is this kind of a Bug in the Foundation framework?
Thanks and greetings,
Alex.

NSParagraphStyle line spacing ignored

A simple test that is failed: Make a new project with just one subview (UITextView) and put the following in:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.f;
paragraphStyle.lineSpacing = 100.f;
paragraphStyle.minimumLineHeight = 200.f;
paragraphStyle.maximumLineHeight = 500.f;
UIFont *font = [UIFont fontWithName:#"AmericanTypewriter" size:24.f];
self.textView.attributedText = [[NSAttributedString alloc] initWithString:
#"This is a test.\n Will I pass?" attributes:
#{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : font}];
}
Line spacing is the same as if the attribute were not there. Has anything got this to work successfully? I put in ridiculous numbers just to show that it won't change...
This is a bug in NSHTMLWriter which is the private class which UITextView uses to convert attributedText into HTML. Internally it displays this HTML via a UIWebDocumentView. Read more on the inner workings of UITextView in my writeup here: http://www.cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/
The problem comes from an easy to miss speciality in the font CSS shorthand. If you specify a pixel size with the font shorthand then this sets BOTH the font-size as well as the line-height. Since NSHTMLWriter puts the font AFTER the line-height this causes the line-height to be cancelled out by the font size.
See here for my Radar which includes the full analysis of the bug: http://www.cocoanetics.com/2012/12/radar-uitextview-ignores-minimummaximum-line-height-in-attributed-string/
I suggest you file a bug report as well and mention my Radar #12863734.
I don't know if this is enough for your purposes but I could adjust the line spacing by setting the minimum and maximum line height. Furthermore to use a font I put it into the font property of the text view rather than passing it as the value of NSFontAttributeName in the attributes dictionary. (Maybe this part is not (well) documented?)
About your attributes
lineSpacing is calculated from the bottom of the line to the bottom of the upper line and that space is constrained to values between minimumLineHeight and miximumLineHeight. What I am trying to say is that maybe some values in your attributes are cancelling or overriding others.
Also if you need to just adjust the spacing between line you probably don't need to use paragraphStyle.lineHeightMultiple :)
The code
This worked for me:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 35.f;
paragraphStyle.maximumLineHeight = 35.f;
UIFont *font = [UIFont fontWithName:#"AmericanTypewriter" size:18.f];
NSString *string = #"This is a test.\nWill I pass?\n日本語のもじもあるEnglish\nEnglish y Español";
NSDictionary *attributtes = #{
NSParagraphStyleAttributeName : paragraphStyle,
};
self.textView.font = font;
self.textView.attributedText = [[NSAttributedString alloc] initWithString:string
attributes:attributtes];
Additional Notes
There seems to be a situation with Japanese/Chinesse and maybe other characters mixed with alphabet characters in the same line. It will make that line to have a bigger leading to solve that you need to set up the minimum and maximum line height as I did.
You can see the problem when rendering my example without attributes.
Setting maximumLineHeight seems to resolve this issue for me;
CGFloat fontSize = 22.f;
titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
paragraphStyle.maximumLineHeight = fontSize/2;
titleLabel.attributedText = [[[NSAttributedString alloc]
initWithString:#"This is a test.\nWill I pass?"
attributes: #{ NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : titleLabel.font}]
autorelease];
For this particular string you need to set paragraphSpacing instead. What's about lineSpacing, I believe it's just not supported yet on iOS.
As nacho4d answered, in iOS 6 you need to use minimumLineHeight and maximumLineHeight and set font directly in UITextView, not in NSAttributedString as line height in that case will be overridden.
Please note that when you set font in UITextView, the "editable" property of UITextView should be set to YES, in other case attributed text would not be affected.
These issues are present only in iOS 6. In iOS 7 and above everything is ok;
In my case, none of the paragraph styling was working. The fix was to set the attributed text on the label AFTER doing any frame adjustments on the label. :)

NSTextField add line spacing

I use NSTextField not NSTextView to receive the user input, but I need to custom the font and textColor and line spacing. I use the code below, it's ok for font and color but I don't know how to set a line spacing.
[self.titleField setTextColor:textColor];
[self.titleField setFont:bold14];
And I also use a NSAttributedString to solve the problem:
NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0];
NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic];
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];
the code above is ok to show a attributed string, but when I delete the string in the textfield and start to input, the words come without any attribute.
How can I input a string in NSTextField with custom font, color and line spacing?
It's best to stay with NSTextField's attribute setting methods instead of an NSAttributedString because then it can send the settings to the field editor. Every text field has an NSTextView (most of the time) "Field Editor"; and the field editor is what is doing the editing.
Your NSAttributedString isn't sticking because you're only telling the textfield to temporarily display that one string. When the field editor pops up the text field (cell) passes on its own attributes like textField.font and textField.textColor but never the NSAttributedString's attributes.
It would be best to use an NSTextView to be able to use -setDefaultParagraphStyle because you're editing multiple lines anyways, from what I see. If you can't, because of performance problems or something else, then:
Subclass NSTextFieldCell, because that's what does all the NSTextField work, and override
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
(declared in NSCell) to set up attributes for your field editor the way you want it, so you can send it a line height value through -setDefaultParagraphStyle (and font etc.) yourself. (textObj is the field editor to be set up).